|
| 1 | +# Create your first data model |
| 2 | + |
| 3 | +Cube follows a dataset-oriented data modeling approach, which is inspired by and |
| 4 | +expands upon dimensional modeling. Cube incorporates this approach and provides |
| 5 | +a practical framework for implementing dataset-oriented data modeling. |
| 6 | + |
| 7 | +When building a data model in Cube, you work with two dataset-centric objects: |
| 8 | +**cubes** and **views**. **Cubes** usually represent business entities such as |
| 9 | +customers, line items, and orders. In cubes, you define all the calculations |
| 10 | +within the measures and dimensions of these entities. Additionally, you define |
| 11 | +relationships between cubes, such as "an order has many line items" or "a user |
| 12 | +may place multiple orders." |
| 13 | + |
| 14 | +**Views** sit on top of a data graph of cubes and create a facade of your entire |
| 15 | +data model, with which data consumers can interact. You can think of views as |
| 16 | +the final data products for your data consumers - BI users, data apps, AI |
| 17 | +agents, etc. When building views, you select measures and dimensions from |
| 18 | +different connected cubes and present them as a single dataset to BI or data |
| 19 | +apps. |
| 20 | + |
| 21 | +<Diagram |
| 22 | + alt="Architecture diagram of queries being sent to cubes and views" |
| 23 | + src="https://ucarecdn.com/bfc3e04a-b690-40bc-a6f8-14a9175fb4fd/" |
| 24 | +/> |
| 25 | + |
| 26 | +## Working with cubes |
| 27 | + |
| 28 | +To begin building your data model, click on <Btn>Enter Development Mode</Btn> in |
| 29 | +Cube Cloud. This will take you to your personal developer space, where you can |
| 30 | +safely make changes to your data model without affecting the production |
| 31 | +environment. |
| 32 | + |
| 33 | +In the previous section, we generated four cubes. To see the data graph of these |
| 34 | +four cubes and how they are connected to each other, click the <Btn>Show |
| 35 | +Graph</Btn> button on the Data Model page. |
| 36 | + |
| 37 | +Let's review the `orders` cube first and update it with additional dimensions |
| 38 | +and measures. |
| 39 | + |
| 40 | +Once you are in developer mode, navigate to the <Btn>Data Model</Btn> and click |
| 41 | +on the `orders.yml` file in the left sidebar inside the `model/cubes` directory |
| 42 | +to open it. |
| 43 | + |
| 44 | +You should see the following content of `model/cubes/orders.yml` file. |
| 45 | + |
| 46 | +```yaml |
| 47 | +cubes: |
| 48 | + - name: orders |
| 49 | + sql_table: ECOM.ORDERS |
| 50 | + |
| 51 | + joins: |
| 52 | + - name: users |
| 53 | + sql: "{CUBE}.USER_ID = {users}.USER_ID" |
| 54 | + relationship: many_to_one |
| 55 | + |
| 56 | + dimensions: |
| 57 | + - name: status |
| 58 | + sql: STATUS |
| 59 | + type: string |
| 60 | + |
| 61 | + - name: id |
| 62 | + sql: ID |
| 63 | + type: number |
| 64 | + primary_key: true |
| 65 | + |
| 66 | + - name: created_at |
| 67 | + sql: CREATED_AT |
| 68 | + type: time |
| 69 | + |
| 70 | + - name: completed_at |
| 71 | + sql: COMPLETED_AT |
| 72 | + type: time |
| 73 | + |
| 74 | + measures: |
| 75 | + - name: count |
| 76 | + type: count |
| 77 | +``` |
| 78 | +
|
| 79 | +As you can see, we already have a `count` measure that we can use to calculate |
| 80 | +the total count of our orders. |
| 81 | + |
| 82 | +Let's add an additional measure to the `orders` cube to calculate only |
| 83 | +**completed orders**. The `status` dimension in the `orders` cube reflects the |
| 84 | +three possible statuses: **processing**, **shipped**, or **completed**. We will |
| 85 | +create a new measure `completed_count` by using a filter on that dimension. To |
| 86 | +do this, we will use a |
| 87 | +[filter parameter](/product/data-modeling/reference/measures#filters) of the |
| 88 | +measure and |
| 89 | +[refer](/product/data-modeling/fundamentals/syntax#referring-to-objects) to the |
| 90 | +existing dimension. |
| 91 | + |
| 92 | +Add the following measure definition to your `model/cubes/orders.yml` file. It |
| 93 | +should be included within the `measures` block. |
| 94 | + |
| 95 | +```yaml |
| 96 | +- name: completed_count |
| 97 | + type: count |
| 98 | + filters: |
| 99 | + - sql: "{CUBE}.status = 'completed'" |
| 100 | +``` |
| 101 | + |
| 102 | +With these two measures in place, `count` and `completed_count`, we can create a |
| 103 | +**derived measure**. Derived measures are measures that you can create based on |
| 104 | +existing measures. Let's create the `completed_percentage` derived measure. |
| 105 | + |
| 106 | +Add the following measure definition to your `model/cubes/orders.yml` file |
| 107 | +within the `measures` block. |
| 108 | + |
| 109 | +```yaml |
| 110 | +- name: completed_percentage |
| 111 | + type: number |
| 112 | + sql: "({completed_count} / NULLIF({count}, 0)) * 100.0" |
| 113 | + format: percent |
| 114 | +``` |
| 115 | + |
| 116 | +Below you can see what your updated `orders` cube should look like with two new |
| 117 | +measures. Feel free to copy this code and paste it into your |
| 118 | +`model/cubes/order.yml` file. |
| 119 | + |
| 120 | +```yaml |
| 121 | +cubes: |
| 122 | + - name: orders |
| 123 | + sql_table: ECOM.ORDERS |
| 124 | +
|
| 125 | + joins: |
| 126 | + - name: users |
| 127 | + sql: "{CUBE}.USER_ID = {users}.USER_ID" |
| 128 | + relationship: many_to_one |
| 129 | +
|
| 130 | + dimensions: |
| 131 | + - name: status |
| 132 | + sql: STATUS |
| 133 | + type: string |
| 134 | +
|
| 135 | + - name: id |
| 136 | + sql: ID |
| 137 | + type: number |
| 138 | + primary_key: true |
| 139 | +
|
| 140 | + - name: created_at |
| 141 | + sql: CREATED_AT |
| 142 | + type: time |
| 143 | +
|
| 144 | + - name: completed_at |
| 145 | + sql: COMPLETED_AT |
| 146 | + type: time |
| 147 | +
|
| 148 | + measures: |
| 149 | + - name: count |
| 150 | + type: count |
| 151 | +
|
| 152 | + - name: completed_count |
| 153 | + type: count |
| 154 | + filters: |
| 155 | + - sql: "{CUBE}.status = 'completed'" |
| 156 | +
|
| 157 | + - name: completed_percentage |
| 158 | + type: number |
| 159 | + sql: "({completed_count} / NULLIF({count}, 0)) * 100.0" |
| 160 | + format: percent |
| 161 | +``` |
| 162 | + |
| 163 | +Click <Btn>Save All</Btn> in the upper corner to save changes to the data model. |
| 164 | +Now, you can navigate to Cube’s Playground. The Playground is a web-based tool |
| 165 | +that allows you to query your data without connecting any tools or writing any |
| 166 | +code. It's the fastest way to explore and test your data model. |
| 167 | + |
| 168 | +You can select measures and dimensions from different cubes in playground, |
| 169 | +including your newly created `completed_percentage` measure. |
| 170 | + |
| 171 | +## Working with views |
| 172 | + |
| 173 | +When building views, we recommend following entity-oriented design and |
| 174 | +structuring your views around your business entities. Usually, cubes tend to be |
| 175 | +normalized entities without duplicated or redundant members, while views are |
| 176 | +denormalized entities where you pick as many measures and dimensions from |
| 177 | +multiple cubes as needed to describe a business entity. |
| 178 | + |
| 179 | +Let's create our first view, which will provide all necessary measures and |
| 180 | +dimensions to explore orders. Views are usually located in the `views` folder |
| 181 | +and have a `_view` postfix. |
| 182 | + |
| 183 | +Create `model/views/orders_view.yml` with the following content: |
| 184 | + |
| 185 | +```yaml |
| 186 | +views: |
| 187 | + - name: orders_view |
| 188 | +
|
| 189 | + cubes: |
| 190 | + - join_path: orders |
| 191 | + includes: |
| 192 | + - status |
| 193 | + - created_at |
| 194 | + - count |
| 195 | + - completed_count |
| 196 | + - completed_percentage |
| 197 | +
|
| 198 | + - join_path: orders.users |
| 199 | + prefix: true |
| 200 | + includes: |
| 201 | + - city |
| 202 | + - age |
| 203 | + - state |
| 204 | +``` |
| 205 | + |
| 206 | +When building views, you can leverage the `cubes` parameter, which enables you |
| 207 | +to include measures and dimensions from other cubes in the view. You can build |
| 208 | +your view by combining multiple joined cubes and specifying the path by which |
| 209 | +they should be joined for that particular view. |
| 210 | + |
| 211 | +After saving, you can experiment with your newly created view in the Playground. |
| 212 | +In the next section, we will learn how to query our `orders_view` using a BI |
| 213 | +tool. |
0 commit comments