Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Building hierarchy with pre-agregated values

I have 3 dimentional tables in my power BI model: 
Continent
Continnet_pkContinent_name
11Europa
22Asia
 
Country
Country_PKCountry_name
101Germany
202Austria
 
City
City_PKCity_name
111Berlin
222Vienna
 
I also have fact table Rating

ForeignKeyScore
11100
22200
1011100
2022200
111111000
222222000
 
ForeignKey will join with each of the DIM table on one value (so, basically relationship is 1:1 with each DIM)
The values in Rating table are already calculated and shouldn't be agregated in the report on any way. 
 
Now I need to create matrix visual that will show hierarchy Continent>Country>City and apply to each respective value its rating.
 
Result:
NameScore
- Europa100
    - Germany101
        - Berlin    111000
    - Austria202
        - Vienna222000
Asia202

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
1 ACCEPTED SOLUTION
Anonymous
Not 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

vyiruanmsft_2-1697012902208.png

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
                    )
                )
        )
    )

vyiruanmsft_0-1697012816169.png

Best Regards

View solution in original post

4 REPLIES 4
Anonymous
Not 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

vyiruanmsft_2-1697012902208.png

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
                    )
                )
        )
    )

vyiruanmsft_0-1697012816169.png

Best Regards

Anonymous
Not 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?  

Anonymous
Not 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:

vyiruanmsft_0-1698308013198.png

Best Regards

lbendlin
Super User
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.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors