Forum Discussion
Building hierarchy with pre-agregated values
| Continnet_pk | Continent_name |
| 11 | Europa |
| 22 | Asia |
| Country_PK | Country_name |
| 101 | Germany |
| 202 | Austria |
| City_PK | City_name |
| 111 | Berlin |
| 222 | Vienna |
| ForeignKey | Score |
| 11 | 100 |
| 22 | 200 |
| 101 | 1100 |
| 202 | 2200 |
| 111 | 111000 |
| 222 | 222000 |
| Name | Score |
| - Europa | 100 |
| - Germany | 101 |
| - Berlin | 111000 |
| - Austria | 202 |
| - Vienna | 222000 |
| Asia | 202 |
I have tried different ways with SWITCH and adding level of hierarchy to manipulate with it, but so far I am not even close to the resolution.
Would appreciate any support!
Many thanks in advance
- Anonymous2 years ago
Hi Anonymous ,
I created a sample pbix file(see the attachment), please check if that is what you want.
1. Create a dimension to explain the hierarchies among continent, country and city
2. Create a measure as below to get the score
Measure = VAR _selcont = SELECTEDVALUE ( 'Table'[Continnet_pk] ) VAR _selcountry = SELECTEDVALUE ( 'Table'[Country_PK] ) VAR _selcity = SELECTEDVALUE ( 'Table'[City_PK] ) RETURN CALCULATE ( SUM ( 'Rating'[Score] ), FILTER ( 'Rating', 'Rating'[ForeignKey] = IF ( ISINSCOPE ( 'Continent'[Continent_name] ) && NOT ( ISINSCOPE ( 'Country'[Country_name] ) ) && NOT ( ISINSCOPE ( 'City'[City_name] ) ), _selcont, IF ( ISINSCOPE ( 'Continent'[Continent_name] ) && ISINSCOPE ( 'Country'[Country_name] ) && NOT ( ISINSCOPE ( 'City'[City_name] ) ), _selcountry, _selcity ) ) ) )Best Regards
4 Replies
- AnonymousNot applicable
Hi Anonymous ,
I created a sample pbix file(see the attachment), please check if that is what you want.
1. Create a dimension to explain the hierarchies among continent, country and city
2. Create a measure as below to get the score
Measure = VAR _selcont = SELECTEDVALUE ( 'Table'[Continnet_pk] ) VAR _selcountry = SELECTEDVALUE ( 'Table'[Country_PK] ) VAR _selcity = SELECTEDVALUE ( 'Table'[City_PK] ) RETURN CALCULATE ( SUM ( 'Rating'[Score] ), FILTER ( 'Rating', 'Rating'[ForeignKey] = IF ( ISINSCOPE ( 'Continent'[Continent_name] ) && NOT ( ISINSCOPE ( 'Country'[Country_name] ) ) && NOT ( ISINSCOPE ( 'City'[City_name] ) ), _selcont, IF ( ISINSCOPE ( 'Continent'[Continent_name] ) && ISINSCOPE ( 'Country'[Country_name] ) && NOT ( ISINSCOPE ( 'City'[City_name] ) ), _selcountry, _selcity ) ) ) )Best Regards
- AnonymousNot applicable
Hi v-yiruan-msft ,
Thanks for the solution! It does the job.
The only issue I have found (after testing this unfortunately), that the same city can be in both countries through different continents as well. Is it still possible to fit it into this measure?- AnonymousNot applicable
Hi Anonymous ,
Thanks for your feedback. Do you mean that one city can be belong to multiple continents? If yes, then you can add these info in the dimension table. For example: city aa, it has two continents: a1 and a2. And its country is a. Then you can add the data in the dimension table as below:
Best Regards
- lbendlin
Super User
Refactor your data model. Your Country table is a sub-dimension of the Continent table so it should contain the foreign key (and the score if you want).
Worst case you can denormalize the whole thing into a City - Country - Continent table. The cardinality is still small enough for that to work.