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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


Go to My LinkedIn Page


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.