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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
TOK
Helper II
Helper II

DAX Measure for previous values based on dimension

I am struggeling with creating a measure with DAX.
I have an existing measure per two dimensions (ChainStoreID, ClusterID)

Now I want my measure to follow two simple rules.

1. If there is a blank value, get the next filled value (ordered by ClusterId) for this chainstore.

egthere are three blank values (116, 1 ; 117, 7 ; 117, 8). As 116, 1 has no lower ClusterId, it should remain empty.
ClusterId 7 and 8 for ChainStoreId 117 should be filled with 87.

2. The current value should be the same or higher than the value of the previous clusterId.

eg. ClusterID 6 (88) is lower than ClusterId 5 (93), so it should be 93.

Current situation:

TOK_1-1648550764528.png

Expectation:

ChainStoreId12345678
116 72748793939393
1176877798788888888
1 ACCEPTED SOLUTION

@TOK 
Try this https://www.dropbox.com/t/fEVKUullXZ43Unud

 

New Value = 
VAR CurrentValue = 
    Data[Value]
VAR CurrentClusterId =
    Data[ClusterId]
VAR CurrentStoreTable =
    CALCULATETABLE ( Data, ALLEXCEPT ( Data, Data[ChainStoreId] ) )
VAR PreviousClustersTable =
    FILTER ( CurrentStoreTable, Data[ClusterId] < CurrentClusterId )
VAR MaxValuePerStore =
    MAXX ( PreviousClustersTable, Data[Value] )
VAR ClusterIdOfMaxValue =
    CALCULATE ( 
        MAX ( Data[ClusterId] ), 
        PreviousClustersTable,
        Data[Value] = MaxValuePerStore 
    )
VAR Result =
    IF ( 
        CurrentValue < MaxValuePerStore
            && CurrentClusterId > ClusterIdOfMaxValue,
        MaxValuePerStore,
        CurrentValue
    )
RETURN
   Result

 

 

View solution in original post

5 REPLIES 5
TOK
Helper II
Helper II

I tried both of your solutions and a calculated column worked best for me. 

But I guess my example wasn't specific enough. It works for Max(Value) having Max(ClusterID), but thats not always the case. ChainStoreID = 113 should return 78 for ClusterID 4 and 5, but our if clause won't return the expected as 78 is not max(Value) nor max(ClusterId).

TOK_0-1648558743879.png

Thanks for your help!

@TOK 
Ok you mean the maximum previous value right? This can be fixed

@TOK 
Try this https://www.dropbox.com/t/fEVKUullXZ43Unud

 

New Value = 
VAR CurrentValue = 
    Data[Value]
VAR CurrentClusterId =
    Data[ClusterId]
VAR CurrentStoreTable =
    CALCULATETABLE ( Data, ALLEXCEPT ( Data, Data[ChainStoreId] ) )
VAR PreviousClustersTable =
    FILTER ( CurrentStoreTable, Data[ClusterId] < CurrentClusterId )
VAR MaxValuePerStore =
    MAXX ( PreviousClustersTable, Data[Value] )
VAR ClusterIdOfMaxValue =
    CALCULATE ( 
        MAX ( Data[ClusterId] ), 
        PreviousClustersTable,
        Data[Value] = MaxValuePerStore 
    )
VAR Result =
    IF ( 
        CurrentValue < MaxValuePerStore
            && CurrentClusterId > ClusterIdOfMaxValue,
        MaxValuePerStore,
        CurrentValue
    )
RETURN
   Result

 

 

tamerj1
Super User
Super User

Hi @TOK 
Here is a sample file with the solutionhttps://www.dropbox.com/t/n3qHIrFTHyVIexTR

You can create a new calculated column 

New Value = 
VAR CurrentValue = 
    Data[Value]
VAR CurrentClusterId =
    Data[ClusterId]
VAR CurrentStoreTable =
    CALCULATETABLE ( Data, ALLEXCEPT ( Data, Data[ChainStoreId] ) )
VAR MaxValuePerStore =
    MAXX ( CurrentStoreTable, Data[Value] )
VAR ClusterIdOfMaxValue =
    CALCULATE ( 
        MAX ( Data[ClusterId] ), 
        CurrentStoreTable,
        Data[Value] = MaxValuePerStore 
    )
VAR Result =
    IF ( 
        CurrentValue < MaxValuePerStore
            && CurrentClusterId > ClusterIdOfMaxValue,
        MaxValuePerStore,
        CurrentValue
    )
RETURN
   Result
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

Value measure: = 
SUM( Data[Value] )

 

Desired outcome measure: =
VAR currentclusterid =
    MAX ( 'Cluster'[ClusterID] )
VAR currentchainstoreid =
    MAX ( ChainStore[ChainStoreID] )
VAR currentvalue = [Value measure:]
VAR maxvalueperchainstore =
    MAXX (
        FILTER (
            ALL ( Data ),
            Data[ChainStoreID] = currentchainstoreid
                && Data[ClusterID] <= currentclusterid
        ),
        Data[Value]
    )
RETURN
    IF (
        currentvalue <= maxvalueperchainstore,
        maxvalueperchainstore,
        currentvalue
    )

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.