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
kkalyanrr
Helper V
Helper V

Dynamic measures( Sum and Difference) for based on Slicer selection

Hello,

I'm trying to achieve the following result from the given dataset, can you help me with the DAX.
When I select "Income"in the slicer, we should get the sum of the amount values.
When I select "Income" and any of the these "Expenses"/"Other Expenses"/"COst of Sales", we should get difference in the amount value.
When I select any of these("Expense"/"Other Expense"/"Cost of Sales") and any of these"Expense"/"Other Expense"/"Cost of Sales" , again we should get the sum of the respective amounts.

Thanks.

 

CategoryNameGLDateAmount
Income01-Jul-19-1,994.24
Expenses01-Jul-19236.64
Other Expenses01-Jul-1928,338.00
Cost of Sales01-Jul-1914,789.34
Income01-Jul-1931,858.65
Expenses01-Aug-193,10,951.91
Other Expenses01-Aug-19-46,357.47
Cost of Sales01-Aug-1919,582.30
Income01-Aug-19324.16
Expenses01-Aug-1950000
Other Expenses01-Aug-19-2,699.24
Cost of Sales01-Aug-19260.47
Income01-Aug-19-1,451.85
Expenses01-Jul-195,730.00
Other Expenses01-Aug-193,038.00
Cost of Sales01-Jul-19200
Income01-Sep-192000
Expenses01-Sep-19-2,24,941.14
Other Expenses01-Sep-19659.48
Cost of Sales01-Sep-1965,577.28
Income01-Sep-1913,482.88
Expenses01-Sep-196,658.00
Other Expenses01-Sep-19-1,083.40
Cost of Sales01-Sep-19-2,699.24
Income01-Sep-19253
Expenses01-Aug-195,910.00
Other Expenses01-Sep-1965,577.28
Cost of Sales01-Sep-1913,482.88
1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @kkalyanrr ,

 

Please try this measure after adding a new table for slicer:

Measure =
VAR _all =
    ALLSELECTED ( forSlicer[Type] )
VAR countSele =
    COUNTROWS ( FILTER ( 'forSlicer', 'forSlicer'[Type] IN _all ) )
VAR _income =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( 'Table', 'Table'[CategoryName] = "Income" )
    )
VAR _sum =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( 'Table', 'Table'[CategoryName] IN _all )
    )
RETURN
    IF (
        countSele = 1
            && "Income" IN _all,
        _income,
        IF ( countSele >= 2 && "Income" IN _all, _income * 2 - _sum, _sum )
    )

 

My final output looks like this : ( measure in the lower left corner is  just for test)

 

11.23.3.1.gif

 

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

View solution in original post

2 REPLIES 2
v-eqin-msft
Community Support
Community Support

Hi @kkalyanrr ,

 

Please try this measure after adding a new table for slicer:

Measure =
VAR _all =
    ALLSELECTED ( forSlicer[Type] )
VAR countSele =
    COUNTROWS ( FILTER ( 'forSlicer', 'forSlicer'[Type] IN _all ) )
VAR _income =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( 'Table', 'Table'[CategoryName] = "Income" )
    )
VAR _sum =
    CALCULATE (
        SUM ( 'Table'[Amount] ),
        FILTER ( 'Table', 'Table'[CategoryName] IN _all )
    )
RETURN
    IF (
        countSele = 1
            && "Income" IN _all,
        _income,
        IF ( countSele >= 2 && "Income" IN _all, _income * 2 - _sum, _sum )
    )

 

My final output looks like this : ( measure in the lower left corner is  just for test)

 

11.23.3.1.gif

 

Here is the pbix file.

 

Did I answer your question ? Please mark my reply as solution. Thank you very much.
If not, please upload some insensitive data samples and expected output.

 

Best Regards,
Eyelyn Qin

amitchandak
Super User
Super User

@kkalyanrr , Try a measure like.

measure =
var _1 = countx(filter(allselected(Table),Table[Category] ="Income"),Table[Category])
var _2 = countx(filter(allselected(Table),Table[Category] <> "Income"),Table[Category])
return
if(isblank(_1) || isblank(_2), Sum(Table[Amount]), calculate(sumx(Table, if(Table[Category] ="Income", Table[Amount],-1* Table[Amount]))))

 

Better to have a category an independent slicer

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.