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
Mpcz
Frequent Visitor

Can I group only by months which are in sequence?

Hi All,

 

I'm quite new to the Power BI tool and I'm not sure how to perform the below grouping (if it's even possible).

 

I have a very simple dataset which looks like that:

Mpcz_0-1657557757833.png

I would like to aggregate the date by user and reduce the amount of entries. I tried to group by [User] with picking the MIN date from [Start Date] and MAX from [End Date] but the issues is that I would like to group only the months which are in sequence (are next to each other in calendar). Based on the above example I would like to achieve the below:

 

Mpcz_1-1657558214234.png

 I will really appreciate help to determine if thats even possible in Power Query Editor.

 

Thank you in advance! 

 

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Mpcz 
Here is a sample file with the proposed solution https://we.tl/t-lKvepWSJSd

This includes creating two calculated columns 

2.png

Rank = 
VAR CurrentRant = 
    RANKX ( Data, Data[Start Date],, ASC, Dense )
VAR AllDates = 
    CALENDAR ( MIN ( Data[Start Date] ), MAX ( Data[End Date] ) )
VAR DatesWithRank = 
    ADDCOLUMNS ( 
        AllDates,
        "@Rank", RANKX ( AllDates, YEAR ( [Date] ) * 100 + MONTH ( [Date] ),, ASC, Dense )
    )
VAR RealRank =
    MAXX ( FILTER ( DatesWithRank, [Date] = Data[Start Date] ), [@Rank] )
RETURN
    RealRank - CurrentRant

3.png

Index = RANKX ( Data, Data[Rank],, ASC, Dense )

Then the measures would simply be

StartDate = MIN ( Data[Start Date] )
EndDate = MAX ( Data[End Date] )

4.png

View solution in original post

4 REPLIES 4
tamerj1
Super User
Super User

Hi @Mpcz 
Here is a sample file with the proposed solution https://we.tl/t-lKvepWSJSd

This includes creating two calculated columns 

2.png

Rank = 
VAR CurrentRant = 
    RANKX ( Data, Data[Start Date],, ASC, Dense )
VAR AllDates = 
    CALENDAR ( MIN ( Data[Start Date] ), MAX ( Data[End Date] ) )
VAR DatesWithRank = 
    ADDCOLUMNS ( 
        AllDates,
        "@Rank", RANKX ( AllDates, YEAR ( [Date] ) * 100 + MONTH ( [Date] ),, ASC, Dense )
    )
VAR RealRank =
    MAXX ( FILTER ( DatesWithRank, [Date] = Data[Start Date] ), [@Rank] )
RETURN
    RealRank - CurrentRant

3.png

Index = RANKX ( Data, Data[Rank],, ASC, Dense )

Then the measures would simply be

StartDate = MIN ( Data[Start Date] )
EndDate = MAX ( Data[End Date] )

4.png

Mpcz
Frequent Visitor

That's a big thank you! It's not the exact solution as I needed to do the transformation in power query but what I did, I replicated the steps from you measure in power query and it works like charm!

Jihwan_Kim
Super User
Super User

Hi,

I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Untitled.png

 

New Table = 
VAR _newtable =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Data[Start Date], Data[End Date], User[User] ),
        "@sortordernumber",
            MAXX (
                FILTER ( 'Calendar', 'Calendar'[Month start CC] = Data[Start Date] ),
                'Calendar'[Month-Year sort order CC]
            ),
        "@previoussortordernumber",
            MAXX (
                FILTER (
                    'Calendar',
                    'Calendar'[Month start CC]
                        =
                        VAR _currentstartdate = Data[Start Date]
                        VAR _currentuser = User[User]
                        VAR _previousstartdate =
                            MAXX (
                                FILTER (
                                    Data,
                                    Data[Start Date] < _currentstartdate
                                        && Data[User] = _currentuser
                                ),
                                Data[Start Date]
                            )
                        RETURN
                            _previousstartdate
                ),
                'Calendar'[Month-Year sort order CC]
            )
    )
VAR _addconditioncolumn =
    ADDCOLUMNS (
        _newtable,
        "@condition",
            IF ( [@sortordernumber] - [@previoussortordernumber] = 1, 0, 1 )
    )
VAR _cumulatecondition =
    ADDCOLUMNS (
        _addconditioncolumn,
        "@cumulate",
            SUMX (
                FILTER (
                    _addconditioncolumn,
                    Data[Start Date] <= EARLIER ( Data[Start Date] )
                        && User[User] = EARLIER ( User[User] )
                ),
                [@condition]
            )
    )
RETURN
    SUMMARIZE (
        GROUPBY (
            _cumulatecondition,
            User[User],
            [@cumulate],
            "@minstartdate", MINX ( CURRENTGROUP (), Data[Start Date] ),
            "@maxenddate", MAXX ( CURRENTGROUP (), Data[End Date] )
        ),
        User[User],
        [@minstartdate],
        [@maxenddate]
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

amitchandak
Super User
Super User

@Mpcz , refer if one of the 2 can help

https://community.powerbi.com/t5/Community-Blog/Power-BI-Continuous-Streak-With-More-Than-a-One-Day-...

https://community.powerbi.com/t5/Community-Blog/Power-BI-Continuous-Streak-With-One-Day-Break/ba-p/1...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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.