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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


Go to My LinkedIn Page


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors