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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
nor303
Helper III
Helper III

dax for changes in a time series and presentations in matrix

Hi, I have the following dataset:

PeriodeKundenrRatingSaldo ChangenrChangeID
31.12.20241110 3Better
31.12.20242211 1Better
31.12.20243112 3Better
31.12.20244Default13 DefaultDefault
31.12.20245214 0Unchanged
31.12.20246315 0Unchanged
31.12.20247416 -1Worse
31.12.20248517 -1Worse
31.12.20249418 0Unchanged
31.12.202410219 2Better
31.12.202411320 2Better
31.12.202412421 -2Worse
31.12.202413522 -3Worse
31.12.202414223 1Better
31.12.202415324 1Better
31.12.202422211 NewNew
31.12.2023149 1Better
31.12.20232310 -1Worse
31.12.20233411 -2Worse
31.12.20234512 -2Worse
31.12.20235213 2Better
31.12.20236314 2Better
31.12.20237315 1Better
31.12.20238416 1Better
31.12.20239417 -2Unchanged
31.12.202310418 -2Worse
31.12.202311519 -2Worse
31.12.202312220 NewNew
31.12.202313221 NewNew
31.12.202314322 NewNew
31.12.202315423 NewNew
31.12.202316524 NewNew
31.12.202317225 NewNew
31.12.202318326 NewNew
31.12.202319427 NewNew
31.12.202320328 NewNew
31.12.202321429 NewNew
31.12.2022158 NewNew
31.12.2022229 NewNew
31.12.20223210 NewNew
31.12.20224311 NewNew
31.12.20225412 NewNew
31.12.20226513 NewNew
31.12.20227414 NewNew
31.12.20228515 NewNew
31.12.20229216 NewNew
31.12.202210217 NewNew
31.12.202211318 NewNew

 

Column F and G are calculated. I want to write a dax that can give me those values. Or even better! The change between periods chosen by a slicer:) 
I also want to be able to present it like this:

 To rating12345UnchangedWorseBetterDefaultGone
 From rating         
20241     00  0
2 25 212225430 25
3 341516 151634 54
43219241817181775 56
5 1320 00 331324
 Sum     587614213159

 

I have only done it for the period 2023-2024, but here it also would be nice to make it dynamic between chosen periodes. 

As you can see this also gives me a problem. How about those Kundenr. that are gone. Like customer 16... between 2023-2024. They are presented in the matrix as gone.

 

Any ideas on how to solve this would be highly appreciated, thanks!

Im sorry for not knowing have to upload the data if that would help:/

2 REPLIES 2
DataNinja777
Super User
Super User

Hi @nor303 

 

To solve your problem, we can use DAX to dynamically calculate the transitions between the periods selected by a slicer, and to handle scenarios such as customers that are no longer present ("Gone"). We'll first calculate the ChangeNr column, which represents the difference in ratings between the current and previous periods for each Kundenr. This can be achieved with the following DAX formula:

ChangeNr = 
VAR CurrentPeriod = MAX('Table'[Periode])
VAR PreviousPeriod = CALCULATE(MAX('Table'[Periode]), 'Table'[Periode] < CurrentPeriod)
VAR CurrentRating = MAX('Table'[Rating])
VAR PreviousRating = CALCULATE(MAX('Table'[Rating]), 'Table'[Periode] = PreviousPeriod && 'Table'[Kundenr] = EARLIER('Table'[Kundenr]))
RETURN 
    IF(
        ISBLANK(PreviousRating),
        BLANK(),
        CurrentRating - PreviousRating
    )

Next, we calculate the ChangeID column to categorize the change (e.g., "Better," "Worse," "Unchanged"). This column determines the nature of the change based on the value of ChangeNr. The formula for this is as follows:

ChangeID = 
VAR Change = [ChangeNr]
RETURN 
    SWITCH(
        TRUE(),
        ISBLANK(Change), "New",
        Change > 0, "Better",
        Change < 0, "Worse",
        Change = 0, "Unchanged",
        "Default"
    )

For identifying "Gone" customers (those present in the previous period but not in the current period), we can use a measure. This measure checks whether a customer exists in the earlier period but not in the later period:

Gone = 
VAR CurrentPeriod = MAX('Table'[Periode])
VAR PreviousPeriod = CALCULATE(MAX('Table'[Periode]), 'Table'[Periode] < CurrentPeriod)
RETURN 
    IF(
        NOT(ISBLANK(CALCULATE(MAX('Table'[Kundenr]), 'Table'[Periode] = PreviousPeriod))) &&
        ISBLANK(CALCULATE(MAX('Table'[Kundenr]), 'Table'[Periode] = CurrentPeriod)),
        1,
        0
    )

To create a transition matrix that dynamically calculates the "From Rating" and "To Rating" based on the selected periods, we use the following measure. This measure evaluates the rating transitions for each Kundenr:

Transition = 
VAR CurrentPeriod = MAX('Table'[Periode])
VAR PreviousPeriod = CALCULATE(MAX('Table'[Periode]), 'Table'[Periode] < CurrentPeriod)
VAR FromRating = CALCULATE(MAX('Table'[Rating]), 'Table'[Periode] = PreviousPeriod && 'Table'[Kundenr] = EARLIER('Table'[Kundenr]))
VAR ToRating = CALCULATE(MAX('Table'[Rating]), 'Table'[Periode] = CurrentPeriod && 'Table'[Kundenr] = EARLIER('Table'[Kundenr]))
RETURN 
    IF(
        ISBLANK(FromRating) || ISBLANK(ToRating),
        BLANK(),
        FromRating & " -> " & ToRating
    )

For the matrix visualization, we use the calculated From Rating and To Rating columns as the rows and columns, respectively. The values in the matrix represent the count of Kundenr for each transition. Additionally, we create a measure to count customers classified as "Gone" for a selected period:

Gone Customers Count = 
CALCULATE(
    COUNTROWS('Table'),
    'Table'[Gone] = 1
)

This approach allows for a dynamic and interactive matrix visualization where the transitions and categories adjust based on the slicer selection. By incorporating measures for detecting "Gone" customers and dynamically calculating transitions, you can create a complete and responsive solution in Power BI. Let me know if further clarification or adjustments are needed!

 

Best regards,

Thanks so much, but I have some problems still: Changenr. calculates the "wrong" way for me. I get numbers for period 2023 2022 and not 2024. It should be for 2024 2023 and 2022 are "New". 
I also get a error with the ChangeID column with sircular reference. Any Ideas on have to solve this?

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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