Forum Discussion

Someone22's avatar
Someone22
Helper I
1 year ago
Solved

Create Matrix or Table with compare data between dates in PowerBi

Hello Experts,   Could you please support me? I can not solve it in PowerBi.   I have a table with demands below. The columns are the months(with format date), de rows is the days when we needed ...
  • DataNinja777's avatar
    1 year ago

    Hi Someone22 ,

     

    To calculate the change in demand between consecutive days for each month in Power BI, you need to reshape your data first using Power Query. Start by unpivoting all the month columns (like 2025.May, 2025.June, etc.) so they become values in a single column instead of being separate headers. This transforms your table into a long format with columns for the Date, Month, and Demand.

    Once loaded into Power BI in this format, you can create a DAX measure that calculates the difference in demand between the current row and the previous date for the same month. Here's the DAX measure you can use:

    Demand Change = 
    VAR CurrentDate = MAX('YourTable'[Date])
    VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])
    VAR CurrentDemand = MAX('YourTable'[Demand])
    
    VAR PrevDate =
        CALCULATE(
            MAX('YourTable'[Date]),
            FILTER(
                ALL('YourTable'),
                'YourTable'[Date] < CurrentDate &&
                'YourTable'[Month] = CurrentMonth
            )
        )
    
    VAR PrevDemand =
        CALCULATE(
            MAX('YourTable'[Demand]),
            'YourTable'[Date] = PrevDate &&
            'YourTable'[Month] = CurrentMonth
        )
    
    RETURN
        IF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)
    

    Replace 'YourTable' with the name of your actual table. This measure compares each date’s demand to the demand from the previous date for the same month and returns the difference. Then, place Date in the rows, Month in the columns, and this measure as the value in a Matrix visual. This will give you exactly what your Excel mock-up shows—highlighting daily changes across months. You can then apply conditional formatting to match your Excel color coding.

     

    Best regards,

  • Someone22's avatar
    Someone22
    1 year ago

    Hello DataNinja777 ,

    Based on your DAX, i start to thinking about the operation of it. After some hours i found the right changes and now it is working :D. I changed 2 MAX to SUM and it is perfect(RED highlight).

     

    Demand Change =
    VAR CurrentDate = MAX('YourTable'[Date])
    VAR CurrentMonth = SELECTEDVALUE('YourTable'[Month])
    VAR CurrentDemand = SUM('YourTable'[Demand])

    VAR PrevDate =
        CALCULATE(
            MAX('YourTable'[Date]),
            FILTER(
                ALL('YourTable'),
                'YourTable'[Date] < CurrentDate &&
                'YourTable'[Month] = CurrentMonth
            )
        )

    VAR PrevDemand =
        CALCULATE(
            SUM('YourTable'[Demand]),
            'YourTable'[Date] = PrevDate &&
            'YourTable'[Month] = CurrentMonth
        )

    RETURN
        IF(ISBLANK(PrevDemand), BLANK(), CurrentDemand - PrevDemand)
     
     

    Thank you again for your support! 🙂