Forum Discussion

Greg_Z's avatar
Greg_Z
Regular Visitor
4 years ago
Solved

Dynamic Grouping by Date Similar to Excel

Hello Everyone,

 

I am trying to recreate a report that pulls data out of an Access DB into Excel then pivots are created from said data.  I am pulling the data directly from the data source instead of Access so that we can retire the Access DB.  What I am trying to do is a grouping based on month similar to what you can do in an Excel Pivot table.  In the screenshot below you can see that a Starting at and Ending at date is defined (I would like this to be dynamic), the grouping is done by month, and the resulting data is grouped by the Ending at year and showing < 1/1/[Starting at year] for all periods before the Ending at year.  Again, I would prefer this to be dynamic, and would also prefer to show the month name instead of number.  I have figured out that I can create a YYYY MM field that changes dynamically, but that does not give me the month name.  Please let me know if any of this makes sense and if more information is needed.

 

Excel Example

 

 

Power BI Formula Without Month Name

 

Period Grouping =

IF(
YEAR(
[Date]
)<YEAR(
TODAY()
),
CONCATENATE(
"< 1/1/",
YEAR(
TODAY()
)
),
FORMAT(
DATEVALUE(
[Date]
),
"YYYY MM"
)
)

 

Power BI Example Without Month Name

 

 

8 Replies

  • davehus's avatar
    davehus
    Icon for Memorable Member rankMemorable Member

    Hi, see code below to give you what you need. 

    Period Grouping C =

    IF(
    YEAR(
    DateTable[DateKey]
    )<YEAR(
    TODAY()
    ),
    CONCATENATE(
    "< 1/1/",
    YEAR(
    TODAY()
    )
    ),
    FORMAT(
    DATEVALUE(
    DateTable[DateKey]
    ),
    "YYYY MMMM"
    )
    )

     

     

    • Greg_Z's avatar
      Greg_Z
      Regular Visitor

      Hi Davehaus,

       

      Thank you for the reply!  However, I have already tried that and it does not work because I cannot sort by the correct year and month.  The periods need to show in the correct order (i.e. < 1/1/2022 then 2022 January then 2022 February then 2022 March).

      • davehus's avatar
        davehus
        Icon for Memorable Member rankMemorable Member

        Hi, there's a couple ways around this. You can try and sort the calculated column by the date column in the ribbon. Another way would be to do everything in Power query and create an index key to sort by. 

  • davehus's avatar
    davehus
    Icon for Memorable Member rankMemorable Member

    I should just further add that as this is a calculated column, it is computed in advance, therefore if you go back to last year, you won't get the <1/1 prefix for 2020.