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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
yasmeensalah
Frequent Visitor

Grouping dates in a matrix

Hello everyone,

 

I've been trying to get my matrix to aggregate data for years prior or later than this year together. While data from this year is broken down to months Like this:

  202220222022202220222022202220222022202220222022 

 

Prior to 2022JanuaryFebruaryMarch AprilMayJune JulyAugustSeptemberOctoberNovemberDecemberlater than 2022
SC22 9111551141145
WA28 300410106497

 

Is this even a possibility?

1 ACCEPTED SOLUTION

See if this works for you. You need to create a table which has the layout you need. In my example I have a date table, so I am creating the Matrix Layout table referencing the Date Table and adding the Prior and After rows as follows:

Matrix Layout =
VAR _Current =
    CALCULATETABLE (
        SUMMARIZE (
            'Date Table',
            'Date Table'[Year],
            'Date Table'[Month],
            'Date Table'[MonthNum]
        ),
        'Date Table'[Year] = YEAR ( TODAY () )
    )
VAR _otherPeriods =
    { ( "Prior to", YEAR ( TODAY () ), 0 ), ( "After", YEAR ( TODAY () ), 13 ) }
RETURN
    UNION ( _Current, _otherPeriods )

Once the table is loaded, I'm adding a sorting order for the "Year" column using:

Sort = 
SWITCH(
    'Matrix Layout'[MonthNum],
    0,1,
    13, 3,
    2)

To get:

table.png

The model is as follows:

model.png

 

 Sort the "Year" column by "Sort"; Sort the "Month" column by "MonthNum" column.

Create the following measure (I'm using a simple SUM for the calculations, so use whatever you need)

Final Measure =
VAR _Prior =
    CALCULATE (
        [Sum Sales],
        FILTER ( ALL ( 'Date Table' ), 'Date Table'[Year] < YEAR ( TODAY () ) )
    )
VAR _After =
    CALCULATE (
        [Sum Sales],
        FILTER ( ALL ( 'Date Table' ), 'Date Table'[Year] > YEAR ( TODAY () ) )
    )
VAR _Current =
    CALCULATE (
        [Sum Sales],
        TREATAS ( VALUES ( 'Matrix Layout'[Month] ), 'Date Table'[Month] ),
        FILTER ( 'Date Table', 'Date Table'[Year] = YEAR ( TODAY () ) )
    )
RETURN
    SWITCH (
        SELECTEDVALUE ( 'Matrix Layout'[MonthNum] ),
        0, _Prior,
        13, _After,
        _Current
    )

Now create the matrix visual with the fields from the Matrix Layout table, whatever you need as rows and the [Final measure] to get:

res.png

 I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

7 REPLIES 7
PaulDBrown
Community Champion
Community Champion

Does  the year selection need to be dynamic or is it always going to be the current year?





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Hi Paul,

 

So the current year will always be the one broken down into months. So by the next year 2023 would be broken down and 2022 will be aggregated with the previous years (2021, 2020, etc).

 

Thanks for trying to help out

See if this works for you. You need to create a table which has the layout you need. In my example I have a date table, so I am creating the Matrix Layout table referencing the Date Table and adding the Prior and After rows as follows:

Matrix Layout =
VAR _Current =
    CALCULATETABLE (
        SUMMARIZE (
            'Date Table',
            'Date Table'[Year],
            'Date Table'[Month],
            'Date Table'[MonthNum]
        ),
        'Date Table'[Year] = YEAR ( TODAY () )
    )
VAR _otherPeriods =
    { ( "Prior to", YEAR ( TODAY () ), 0 ), ( "After", YEAR ( TODAY () ), 13 ) }
RETURN
    UNION ( _Current, _otherPeriods )

Once the table is loaded, I'm adding a sorting order for the "Year" column using:

Sort = 
SWITCH(
    'Matrix Layout'[MonthNum],
    0,1,
    13, 3,
    2)

To get:

table.png

The model is as follows:

model.png

 

 Sort the "Year" column by "Sort"; Sort the "Month" column by "MonthNum" column.

Create the following measure (I'm using a simple SUM for the calculations, so use whatever you need)

Final Measure =
VAR _Prior =
    CALCULATE (
        [Sum Sales],
        FILTER ( ALL ( 'Date Table' ), 'Date Table'[Year] < YEAR ( TODAY () ) )
    )
VAR _After =
    CALCULATE (
        [Sum Sales],
        FILTER ( ALL ( 'Date Table' ), 'Date Table'[Year] > YEAR ( TODAY () ) )
    )
VAR _Current =
    CALCULATE (
        [Sum Sales],
        TREATAS ( VALUES ( 'Matrix Layout'[Month] ), 'Date Table'[Month] ),
        FILTER ( 'Date Table', 'Date Table'[Year] = YEAR ( TODAY () ) )
    )
RETURN
    SWITCH (
        SELECTEDVALUE ( 'Matrix Layout'[MonthNum] ),
        0, _Prior,
        13, _After,
        _Current
    )

Now create the matrix visual with the fields from the Matrix Layout table, whatever you need as rows and the [Final measure] to get:

res.png

 I've attached the sample PBIX file





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Hi Paul!

 

Thank you so very much, It worked!

 

One small thing though, I couldn't sort the month name by month number. Cause the value "2022" is repeated twice in column Month name. I get this error:

yasmeensalah_0-1659129284344.png

This is what the final table looks like:

yasmeensalah_2-1659129555432.png

How did you get your to sort?

Of course...you're right. It cannot sort beacuase there are two rows with 2022. This may actually be a bug. Let me explain. Originally I built the Matrix Layout with both the "Prior to 2022" and "after 2022" in the Month column. I did the sorting to see the visual. I then decided to move the "pior to" and "After " texts to the Year column, That's probably why it is still sorting the months correctly.

 

Anyway, an easy fix. Use this codefor the Matrix Layout table and you should be able to sort the months correctly (you also need the "sort" column for the years)

Matrix Layout =
VAR _Current =
    CALCULATETABLE (
        SUMMARIZE (
            'Date Table',
            'Date Table'[Year],
            'Date Table'[Month],
            'Date Table'[MonthNum]
        ),
        'Date Table'[Year] = YEAR ( TODAY () )
    )
VAR _otherPeriods =
    {
        ( "Prior", "to " & YEAR ( TODAY () ), 0 ),
        ( "After", YEAR ( TODAY () ), 13 )
    }
RETURN
    UNION ( _Current, _otherPeriods )




Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






That explains it. Thanks again for your support!

You know what I just changed things up a little to make it work: 

yasmeensalah_2-1659133356896.png

 

Final matrix: 

yasmeensalah_1-1659133284083.png

Thanks a lot

 

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.