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 to check the demands. We do not have rule for the dates of days, for example: every second day...etc., if we would like to see, we refresh it.

Days2025.May2025.June2025.July2025.August2025.September
2025.05.285 0005 00010 00013 00015 000
2025.05.2610 00013 0005 0006 0005 000
2025.05.2315 0006 00015 0007 00013 000
2025.05.1913 0007 00013 0008 0006 000
2025.05.186 0008 0006 0004 0007 000
2025.05.167 0004 0007 0005 0008 000
2025.05.108 0005 0008 00015 0004 000
2025.05.094 00015 0004 00010 00010 000

I would like to see in the cells only the changes between the two days in the new other table or matrix. What is it mean: 

 

How can i solve it? I could not find any solution.

 

Thank you in advance!

 

  • 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! 🙂

     

     

     

8 Replies

  • 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
      Helper I

      Hello DataNinja777 ,

      I tried it, working, but it show wrong values. You see in below.

       

      In the database there are other columns, not just Date, Months and Demand. It is cause maybe the problem? But the other columns are needed because of the filters.

       

      Thank you in advance again if you can check what would be the problem!

    • Someone22's avatar
      Someone22
      Helper I

      Hello DataNinja777 ,

      I am new in PBI and i think i missed something really important. Sorry for that. The database in power query, it is not the same what i sent in my first post. The table in my first post is the summary table about raw table from query and i think this is the problem why the dax can not show what i want because in the same Day and Month rows there are more different demands.

       

      So, for an example review my raw database in power query after unpivot the months columns:

      Raw database in power query:

      DaysMaterialTypeCityMonthsDemand
      2025.05.30AXBig2025. May500
      2025.05.30BXSmall2025. May700
      2025.05.30CXMedium2025. May300
      2025.05.30AYBig2025. May500
      2025.05.30BYSmall2025. May600
      2025.05.30CYMedium2025. May900
      2025.05.30AXBig2025.June1100
      2025.05.30BXSmall2025.June1500
      2025.05.30CXMedium2025.June400
      2025.05.30AYBig2025.June700
      2025.05.30BYSmall2025.June320
      2025.05.30CYMedium2025.June675
      2025.05.30AXBig2025.July456
      2025.05.30BXSmall2025.July234
      2025.05.30CXMedium2025.July953
      2025.05.30AYBig2025.July1134
      2025.05.30BYSmall2025.July1054
      2025.05.30CYMedium2025.July454
      2025.05.30AXBig2025.August245
      2025.05.30BXSmall2025.August1034
      2025.05.30CXMedium2025.August103
      2025.05.30AYBig2025.August567
      2025.05.30BYSmall2025.August345
      2025.05.30CYMedium2025.August876
      2025.05.30AXBig2025.September1256
      2025.05.30BXSmall2025.September2210
      2025.05.30CXMedium2025.September222
      2025.05.30AYBig2025.September621
      2025.05.30BYSmall2025.September453
      2025.05.30CYMedium2025.September389
      2025.05.28AXBig2025. May550
      2025.05.28BXSmall2025. May750
      2025.05.28CXMedium2025. May350
      2025.05.28AYBig2025. May550
      2025.05.28BYSmall2025. May650
      2025.05.28CYMedium2025. May950
      2025.05.28AXBig2025.June1150
      2025.05.28BXSmall2025.June1550
      2025.05.28CXMedium2025.June450
      2025.05.28AYBig2025.June750
      2025.05.28BYSmall2025.June370
      2025.05.28CYMedium2025.June725
      2025.05.28AXBig2025.July506
      2025.05.28BXSmall2025.July284
      2025.05.28CXMedium2025.July1003
      2025.05.28AYBig2025.July1184
      2025.05.28BYSmall2025.July1104
      2025.05.28CYMedium2025.July504
      2025.05.28AXBig2025.August295
      2025.05.28BXSmall2025.August1084
      2025.05.28CXMedium2025.August153
      2025.05.28AYBig2025.August617
      2025.05.28BYSmall2025.August395
      2025.05.28CYMedium2025.August926
      2025.05.28AXBig2025.September1306
      2025.05.28BXSmall2025.September2260
      2025.05.28CXMedium2025.September272
      2025.05.28AYBig2025.September671
      2025.05.28BYSmall2025.September503
      2025.05.28CYMedium2025.September439
      2025.05.27AXBig2025. May450
      2025.05.27BXSmall2025. May650
      2025.05.27CXMedium2025. May250
      2025.05.27AYBig2025. May450
      2025.05.27BYSmall2025. May550
      2025.05.27CYMedium2025. May850
      2025.05.27AXBig2025.June1050
      2025.05.27BXSmall2025.June1450
      2025.05.27CXMedium2025.June350
      2025.05.27AYBig2025.June650
      2025.05.27BYSmall2025.June270
      2025.05.27CYMedium2025.June625
      2025.05.27AXBig2025.July406
      2025.05.27BXSmall2025.July184
      2025.05.27CXMedium2025.July903
      2025.05.27AYBig2025.July1084
      2025.05.27BYSmall2025.July1004
      2025.05.27CYMedium2025.July404
      2025.05.27AXBig2025.August195
      2025.05.27BXSmall2025.August984
      2025.05.27CXMedium2025.August53
      2025.05.27AYBig2025.August517
      2025.05.27BYSmall2025.August295
      2025.05.27CYMedium2025.August826
      2025.05.27AXBig2025.September1206
      2025.05.27BXSmall2025.September2160
      2025.05.27CXMedium2025.September172
      2025.05.27AYBig2025.September571
      2025.05.27BYSmall2025.September403
      2025.05.27CYMedium2025.September339
      2025.05.15AXBig2025. May600
      2025.05.15BXSmall2025. May800
      2025.05.15CXMedium2025. May400
      2025.05.15AYBig2025. May600
      2025.05.15BYSmall2025. May700
      2025.05.15CYMedium2025. May1000
      2025.05.15AXBig2025.June1200
      2025.05.15BXSmall2025.June1600
      2025.05.15CXMedium2025.June500
      2025.05.15AYBig2025.June800
      2025.05.15BYSmall2025.June420
      2025.05.15CYMedium2025.June775
      2025.05.15AXBig2025.July556
      2025.05.15BXSmall2025.July334
      2025.05.15CXMedium2025.July1053
      2025.05.15AYBig2025.July1234
      2025.05.15BYSmall2025.July1154
      2025.05.15CYMedium2025.July554
      2025.05.15AXBig2025.August345
      2025.05.15BXSmall2025.August1134
      2025.05.15CXMedium2025.August203
      2025.05.15AYBig2025.August667
      2025.05.15BYSmall2025.August445
      2025.05.15CYMedium2025.August976
      2025.05.15AXBig2025.September1356
      2025.05.15BXSmall2025.September2310
      2025.05.15CXMedium2025.September322
      2025.05.15AYBig2025.September721
      2025.05.15BYSmall2025.September553
      2025.05.15CYMedium2025.September489
      2025.05.05AXBig2025. May350
      2025.05.05BXSmall2025. May550
      2025.05.05CXMedium2025. May150
      2025.05.05AYBig2025. May350
      2025.05.05BYSmall2025. May450
      2025.05.05CYMedium2025. May750
      2025.05.05AXBig2025.June950
      2025.05.05BXSmall2025.June1350
      2025.05.05CXMedium2025.June250
      2025.05.05AYBig2025.June550
      2025.05.05BYSmall2025.June170
      2025.05.05CYMedium2025.June525
      2025.05.05AXBig2025.July306
      2025.05.05BXSmall2025.July84
      2025.05.05CXMedium2025.July803
      2025.05.05AYBig2025.July984
      2025.05.05BYSmall2025.July904
      2025.05.05CYMedium2025.July304
      2025.05.05AXBig2025.August95
      2025.05.05BXSmall2025.August884
      2025.05.05CXMedium2025.August47
      2025.05.05AYBig2025.August417
      2025.05.05BYSmall2025.August195
      2025.05.05CYMedium2025.August726
      2025.05.05AXBig2025.September1106
      2025.05.05BXSmall2025.September2060
      2025.05.05CXMedium2025.September72
      2025.05.05AYBig2025.September471
      2025.05.05BYSmall2025.September303
      2025.05.05CYMedium2025.September239
      2025.05.01AXBig2025. May0
      2025.05.01BXSmall2025. May0
      2025.05.01CXMedium2025. May0
      2025.05.01AYBig2025. May0
      2025.05.01BYSmall2025. May0
      2025.05.01CYMedium2025. May0
      2025.05.01AXBig2025.June0
      2025.05.01BXSmall2025.June0
      2025.05.01CXMedium2025.June0
      2025.05.01AYBig2025.June0
      2025.05.01BYSmall2025.June0
      2025.05.01CYMedium2025.June0
      2025.05.01AXBig2025.July0
      2025.05.01BXSmall2025.July0
      2025.05.01CXMedium2025.July0
      2025.05.01AYBig2025.July0
      2025.05.01BYSmall2025.July0
      2025.05.01CYMedium2025.July0
      2025.05.01AXBig2025.August0
      2025.05.01BXSmall2025.August0
      2025.05.01CXMedium2025.August0
      2025.05.01AYBig2025.August0
      2025.05.01BYSmall2025.August0
      2025.05.01CYMedium2025.August0
      2025.05.01AXBig2025.September0
      2025.05.01BXSmall2025.September0
      2025.05.01CXMedium2025.September0
      2025.05.01AYBig2025.September0
      2025.05.01BYSmall2025.September0
      2025.05.01CYMedium2025.September0

       

      Summary and compare(what i want to see in PBI) matrix:

       

      I am really sorry again :).

      Thank you in advance!

       

    • Someone22's avatar
      Someone22
      Helper I

      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! 🙂

       

       

       

  • v-achippa's avatar
    v-achippa
    Community Support

    Hi Someone22,

     

    Thank you for reaching out to Microsoft Fabric Community.

     

    Based on your description, please follow below steps:

    • Open Power Query Editor select all month columns (2025.May to 2025.September) and click Unpivot Columns.
    • Sort the Days column in descending order.
    • Go to Add Column --> Index Column and add a custom index column from 0 and name it as Index.
    • Duplicate the table and name it PreviousRow and rename its Index column to PreviousIndex.
    • And merge the original table with PreviousRow, on Month = Month and on Index = PreviousIndex + 1.
    • Expand the merged table and extract the previous demand value.
    • After the merge create a new column to calculate the difference:
      = [Demand] - [PreviousRow.Demand]
    • Click Close & Apply and load the data back to power bi.
    • Create a Matrix visual and use Days in Rows, Month in Columns and calculated difference in Values.

    This matrix will now show the demand changes compared to the previous snapshot date for each month.

     

    If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

     

    Thanks and regards,

    Anjan Kumar Chippa

    • Someone22's avatar
      Someone22
      Helper I

      Hello v-achippa ,

      I tried it, but the database is too big and the index step kill the query :(. I can not finish the step because the query freeze.

      • v-achippa's avatar
        v-achippa
        Community Support

        Hi Someone22,

         

        Thank you for the response, when working with large datasets the Index and Merge method can be slow or even freeze the power query. So please follow this below performance optimized approach:

        • Open Power Query Editor select all month columns (2025.May to 2025.September) and click Unpivot Columns. Rename columns to Days, Month, Demand.
        • Make sure Days column is in date format, and sort by Month and then Days in descending order
        • Duplicate the table and name it as PreviousRow.
        • In the original table, rename the Days column to CurrentDay. And in the PreviousRow, rename the Days column to PreviousDay.
        • Add a new custom column and name it as NextSnapshotDay and use the below code:
          = Date.AddDays([PreviousDay], 1)
        • Now merge the original table with PreviousRow, on Month = Month and CurrentDay from original table = NextSnapshotDay from PreviousRow
        • Expand the merged PreviousRow table and extract PreviousDay and Demand. Rename the extracted Demand column to PreviousDemand.
        • After the merge add a new column to calculate the difference:
          = [Demand] - [PreviousDemand]
        • Click Close & Apply to load the transformed data.
        • Create a Matrix visual and use CurrentDay in Rows, Month in Columns and calculated difference in Values.

        This approach avoids heavy index operations and performs better on large datasets and It will show the change in demand between the current snapshot and the previous one for each month.

         

        If this post helps, then please consider Accepting as solution to help the other members find it more quickly, don't forget to give a "Kudos" – I’d truly appreciate it! 

         

        Thanks and regards,

        Anjan Kumar Chippa