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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Rank based on Measure for two tables

Hi guys, 

 

I'm having some trouble solving the following issue: 

 

I have two fact tables from different datasets that contain similar information, just for different time stamps. The 2nd table will only contain a subset of Product Keys from the first table that had an update in Period 2, so if the Product Key doesn't appear in the 2nd table, the Cost will be the the same as in Period 1:

 

Table 1:

Department GroupProduct KeyDateCost
11Period 1100
12Period 1200
13Period 1300
24Period 1400
25Period 1500
26Period 1600

 

Table 2:

Department GroupProduct KeyDateCost
11Period 2250
13Period 2350
24Period 2700
26Period 2650

 

Tables are linked with product & date table using the keys. I created a measure for Cost that refers to both tables, see simplified example: 

Cost = IF(ISBLANK(MIN(Table1[Cost])),
        MIN(Table2[Cost]),
        MIN(Table1[Cost]))
 
Now I'm trying to calculate the cost rank of each Product Key within the respective Department Group & Date based on the Cost measure I created. It needs to refer to the measure, because there are some adjustments I do on the data depending on which table it comes from within the measure (Cost measure is simplified). Probably it should create a temporary table that combines Table 1 & Table 2 and fills blanks in Table 2 for missing Cost values in Period 2 (just as the Cost measure does) with the Rank column being what I'm looking for:
 
GroupProduct KeyDateCostRank
11Period 11001
12Period 12002
13Period 13003
24Period 14001
25Period 15002
26Period 16003
11Period 22502
12Period 22001
13Period 23503
24Period 27003
25Period 25001
26Period 26502
 
I hope I explained the problem well enough and someone can help me with this. 
 
Thanks in advance!
2 ACCEPTED SOLUTIONS
v-yadongf-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please create a new table:

Table = 
UNION (
    SELECTCOLUMNS (
        'Table1',
        "Department Group", 'Table1'[Department Group],
        "Product Key", 'Table1'[Product Key],
        "Date", 'Table1'[Date],
        "Cost", 'Table1'[Cost]
    ),
    SELECTCOLUMNS (
        'Table1',
        "Department Group",
            IF (
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
                    = BLANK (),
                'Table1'[Department Group],
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
            ),
        "Product Key", 'Table1'[Product Key],
        "Date2", "Period2",
        "Cost",
            IF (
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
                    = BLANK (),
                'Table1'[Cost],
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
            )
    )
)

 

You will get a table like this:

vyadongfmsft_0-1669108480039.png

 

Create a rank column:

Rank = 
RANKX (
    FILTER (
        'Table',
        'Table'[Department Group] = EARLIER ( 'Table'[Department Group] )
            && 'Table'[Date] = EARLIER ( 'Table'[Date] )
    ),
    'Table'[Cost],
    ,
    ASC
)

 

I think this is the result you want:

vyadongfmsft_1-1669108545753.png

 

 

Best regards,

Yadong Fang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Hi @Anonymous ,

 

I add a new product key in period 2 which does not exist in period 1:

vyadongfmsft_0-1669166815963.png

Create a new table:

 

New_Table = 
var _tab = UNION (
    SELECTCOLUMNS (
        'Table2',
        "Department Group", 'Table2'[Department Group],
        "Product Key", 'Table2'[Product Key],
        "Date", 'Table2'[Date],
        "Cost", 'Table2'[Cost]
    ),
    SELECTCOLUMNS (
        'Table1',
        "Department Group",
            IF (
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
                    = BLANK (),
                'Table1'[Department Group],
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
            ),
        "Product Key", 'Table1'[Product Key],
        "Date2", "Period 2",
        "Cost",
            IF (
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
                    = BLANK (),
                'Table1'[Cost],
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
            )
    )
)
return
DISTINCT(_tab)

 

vyadongfmsft_1-1669166931202.png

 

Create the other new table:

 

 

New_Table2 = 
UNION(
SELECTCOLUMNS (
        'Table1',
        "Department Group", 'Table1'[Department Group],
        "Product Key", 'Table1'[Product Key],
        "Date", 'Table1'[Date],
        "Cost", 'Table1'[Cost]
    ), 
    SELECTCOLUMNS (
        'New_Table',
        "Department Group", 'New_Table'[Department Group],
        "Product Key", 'New_Table'[Product Key],
        "Date", 'New_Table'[Date],
        "Cost", 'New_Table'[Cost]
    ))

 

vyadongfmsft_2-1669167028517.png

 

Create a rank column:

 

Rank = 
RANKX (
    FILTER (
        'New_Table2',
        'New_Table2'[Department Group] = EARLIER ( 'New_Table2'[Department Group] )
            && 'New_Table2'[Date] = EARLIER ( 'New_Table2'[Date] )
    ),
    'New_Table2'[Cost],
    ,
    ASC
)

 

vyadongfmsft_3-1669167081042.png

 

I think this is the result you want:

vyadongfmsft_4-1669167135443.png

Best regards,

Yadong Fang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-yadongf-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please create a new table:

Table = 
UNION (
    SELECTCOLUMNS (
        'Table1',
        "Department Group", 'Table1'[Department Group],
        "Product Key", 'Table1'[Product Key],
        "Date", 'Table1'[Date],
        "Cost", 'Table1'[Cost]
    ),
    SELECTCOLUMNS (
        'Table1',
        "Department Group",
            IF (
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
                    = BLANK (),
                'Table1'[Department Group],
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
            ),
        "Product Key", 'Table1'[Product Key],
        "Date2", "Period2",
        "Cost",
            IF (
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
                    = BLANK (),
                'Table1'[Cost],
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
            )
    )
)

 

You will get a table like this:

vyadongfmsft_0-1669108480039.png

 

Create a rank column:

Rank = 
RANKX (
    FILTER (
        'Table',
        'Table'[Department Group] = EARLIER ( 'Table'[Department Group] )
            && 'Table'[Date] = EARLIER ( 'Table'[Date] )
    ),
    'Table'[Cost],
    ,
    ASC
)

 

I think this is the result you want:

vyadongfmsft_1-1669108545753.png

 

 

Best regards,

Yadong Fang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi Yadong Fang, 

 

Thanks for the input, this is great help!

 

I forgot to mention one additional case: It might happen that Table2 contains Product Keys that have not been existent in Period 1 and thus don't show in Table1.
I see in the formula for the new table that for everything that comes from Table2 you refer to Product Keys from Table1 and then add missing info which would lead to leaving out added Product Keys in Period2. How would I have to change the formula to cover also this additional case?

 

Many thanks in advance!

Hi @Anonymous ,

 

I add a new product key in period 2 which does not exist in period 1:

vyadongfmsft_0-1669166815963.png

Create a new table:

 

New_Table = 
var _tab = UNION (
    SELECTCOLUMNS (
        'Table2',
        "Department Group", 'Table2'[Department Group],
        "Product Key", 'Table2'[Product Key],
        "Date", 'Table2'[Date],
        "Cost", 'Table2'[Cost]
    ),
    SELECTCOLUMNS (
        'Table1',
        "Department Group",
            IF (
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
                    = BLANK (),
                'Table1'[Department Group],
                LOOKUPVALUE (
                    'Table2'[Department Group],
                    'Table2'[Product Key], 'Table1'[Product Key]
                )
            ),
        "Product Key", 'Table1'[Product Key],
        "Date2", "Period 2",
        "Cost",
            IF (
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
                    = BLANK (),
                'Table1'[Cost],
                LOOKUPVALUE ( 'Table2'[Cost], 'Table2'[Product Key], 'Table1'[Product Key] )
            )
    )
)
return
DISTINCT(_tab)

 

vyadongfmsft_1-1669166931202.png

 

Create the other new table:

 

 

New_Table2 = 
UNION(
SELECTCOLUMNS (
        'Table1',
        "Department Group", 'Table1'[Department Group],
        "Product Key", 'Table1'[Product Key],
        "Date", 'Table1'[Date],
        "Cost", 'Table1'[Cost]
    ), 
    SELECTCOLUMNS (
        'New_Table',
        "Department Group", 'New_Table'[Department Group],
        "Product Key", 'New_Table'[Product Key],
        "Date", 'New_Table'[Date],
        "Cost", 'New_Table'[Cost]
    ))

 

vyadongfmsft_2-1669167028517.png

 

Create a rank column:

 

Rank = 
RANKX (
    FILTER (
        'New_Table2',
        'New_Table2'[Department Group] = EARLIER ( 'New_Table2'[Department Group] )
            && 'New_Table2'[Date] = EARLIER ( 'New_Table2'[Date] )
    ),
    'New_Table2'[Cost],
    ,
    ASC
)

 

vyadongfmsft_3-1669167081042.png

 

I think this is the result you want:

vyadongfmsft_4-1669167135443.png

Best regards,

Yadong Fang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Your dax statement will not create a temp table, you need to use summarize or add column functions to create temp table and use that.

Anonymous
Not applicable

I don't really know what you're referring to. Of course it's not creating a temp table, that's what I'm asking help for. Using the measure in the report on the other will do the same from a logic perspective, it fills the blanks for missing values in P2. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.