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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Grouping on two columns according to values sign

Hello,

 

I was wondering if it is possible to do this kind of grouping for two columns based on the sign of the interval values (and how).

The months are selected by a slicer in the report.

 

Thanks for the help

 

GuyInaBall_0-1664789410530.png

 

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @Anonymous 
Please refer to the attached sample file with the solution.

3.png2.png1.png

 

Rank = 
VAR Ranking1 =
    RANKX ( FILTER ( 'Table', 'Table'[Value] > 0 ), 'Table'[Month],, ASC, Dense )
VAR Ranking2 =
    RANKX ( FILTER ( 'Table', 'Table'[Value] < 0 ), 'Table'[Month],, ASC, Dense )
VAR Result =
   IF ( 'Table'[Value] > 0, Ranking2, Ranking1 )
RETURN
    Result
Group = 
VAR CurrentGroupTable = 
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Rank] ) )
VAR Starting = 
    MINX ( CurrentGroupTable, 'Table'[Month] )
VAR Ending = 
    MAXX ( CurrentGroupTable, 'Table'[Month] )
RETURN
    FORMAT ( Starting, "MMMM" ) & " - " & FORMAT ( Ending, "MMMM" )
Average Value = AVERAGE ( 'Table'[Value] )

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hello @tamerj1 & @Jihwan_Kim , thank you for your nice answsers.

@tamerj1's solution is the closest to what I need. I'll just add a new measure so that the period label reflects more precisely the selected period and hid the month group.

Thank you again for the help and the effort, much appreciated

tamerj1
Super User
Super User

Hi @Anonymous 
Please refer to the attached sample file with the solution.

3.png2.png1.png

 

Rank = 
VAR Ranking1 =
    RANKX ( FILTER ( 'Table', 'Table'[Value] > 0 ), 'Table'[Month],, ASC, Dense )
VAR Ranking2 =
    RANKX ( FILTER ( 'Table', 'Table'[Value] < 0 ), 'Table'[Month],, ASC, Dense )
VAR Result =
   IF ( 'Table'[Value] > 0, Ranking2, Ranking1 )
RETURN
    Result
Group = 
VAR CurrentGroupTable = 
    CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[Rank] ) )
VAR Starting = 
    MINX ( CurrentGroupTable, 'Table'[Month] )
VAR Ending = 
    MAXX ( CurrentGroupTable, 'Table'[Month] )
RETURN
    FORMAT ( Starting, "MMMM" ) & " - " & FORMAT ( Ending, "MMMM" )
Average Value = AVERAGE ( 'Table'[Value] )

 

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

It is for creating a new table.

 

Jihwan_Kim_0-1664797027764.png

 

 

New Table = 
VAR _previousvalue =
    ADDCOLUMNS (
        Data,
        "@prev",
            CALCULATE (
                SUM ( Data[Value] ),
                FILTER ( Data, Data[Month number] = EARLIER ( Data[Month number] ) - 1 )
            )
    )
VAR _condition =
    ADDCOLUMNS (
        _previousvalue,
        "@condition",
            IF ( Data[Value] * [@prev] >= 0, 0, 1 )
    )
VAR _group =
    ADDCOLUMNS (
        _condition,
        "@group",
            SUMX (
                FILTER ( _condition, Data[Month number] <= EARLIER ( Data[Month number] ) ),
                [@condition]
            )
    )
VAR _newtable =
    ADDCOLUMNS (
        _group,
        "@month",
            MAXX (
                FILTER (
                    Data,
                    Data[Month number]
                        = MINX ( FILTER ( _group, [@group] = EARLIER ( [@group] ) ), Data[Month number] )
                ),
                Data[Month]
            ) & "~"
                & MAXX (
                    FILTER (
                        Data,
                        Data[Month number]
                            = MAXX ( FILTER ( _group, [@group] = EARLIER ( [@group] ) ), Data[Month number] )
                    ),
                    Data[Month]
                )
    )
VAR _avgcolumn =
    ADDCOLUMNS (
        _newtable,
        "@avgvalue", AVERAGEX ( FILTER ( _group, [@group] = EARLIER ( [@group] ) ), Data[Value] )
    )
RETURN
    SUMMARIZE ( _avgcolumn, [@month], [@avgvalue] )

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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