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
jay_patel
Helper IV
Helper IV

Calculate sum of column based on selected Month from slicer

I have a table which contains data as shown below: 

jay_patel_0-1712986022283.png

Above shown data is just a sample data , in actual data the columns are up to TransactionCurrency12 and fiscal year from 2017 to current fiscal year.

I also have a month-year slicer which has values like Apr-2023, May-2022, Jun-2018 and so on. 

 

Now, I want to display sum in such a manner that if I am selecting Apr-2023 from slicer then, it should display sum of TransactionCurrency01 where fiscal year = 2023 , if May-2022 then sum of TransactionCurrency01 + TransactionCurrency02 where fiscal year = 2022, if Jun-2023 then sum of TransactionCurrency01 + TransactionCurrency02 + TransactionCurrency03 where fiscal year = 2023, if Jul-2021 , then sum of TransactionCurrency01 + TransactionCurrency02 + TransactionCurrency03 + TransactionCurrency04 where fiscal year = 2021 and so on .

 

Currently , I am using below DAX   by writing condition for all month-year which is not feasible:

VAR SelectedMonth = SELECTEDVALUE(Calendar_New2[month year new])
RETURN
SWITCH(
    SelectedMonth,
    "Apr-2023", SUM('copy of GLT0'[TransactionCurrency01]),
    "May-2023", SUM('copy of GLT0'[TransactionCurrency01]) + SUM('copy of GLT0'[TransactionCurrency02]),
    "Jun-2023", SUM('copy of GLT0'[TransactionCurrency01]) + SUM('copy of GLT0'[TransactionCurrency02]) +SUM('copy of GLT0'[TransactionCurrency03]) .... and so on , 
 
 
1 ACCEPTED SOLUTION

@jay_patel This is a bit hacky but works. See PBIX. You really should be using a fiscal calendar for this sort of thing.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@jay_patel So typically you would want to unpivot those columns and that will make your life much easier. Otherwise, you need something like MC Aggregations: https://community.fabric.microsoft.com/t5/Quick-Measures-Gallery/Multi-Column-Aggregations-MC-Aggreg...

 

In your case, if you unpivot your columns and create a calculated column of RIGHT([Attribute],1)+0 then you would have a numeric value that you could use <= in your calculation as a filter.

 

If this doesn't help, post your data as text and what you want to end up with.

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Below is my sample data ,

 

Wholesalers  Fiscal_Year  TransactionCurrency01  TransactionCurrency02  TransactionCurrency03
A2023000
B2023758.452129.562385.11
C2023000
D2023780089.394607.2618454.92
E2022-60060-2933.2
F2022-230454.840-8504
G2022-10549.140-1230.93

 

I also have a month year slicer which has values from Jan-2018 to current month year i.e. Apr-2024

jay_patel_0-1713038550841.png

Now, I want to display sum in such a manner that if I am selecting Apr-2023 from slicer then, it should display sum of TransactionCurrency01 where fiscal year = 2023 ,

if May-2022 then sum of TransactionCurrency01 + TransactionCurrency02 where fiscal year = 2022,

if Jun-2023 then sum of TransactionCurrency01 + TransactionCurrency02 + TransactionCurrency03 where fiscal year = 2023,

if Jul-2021 , then sum of TransactionCurrency01 + TransactionCurrency02 + TransactionCurrency03 + TransactionCurrency04 where fiscal year = 2021 and so on .

Expected output is as shown below :

 

Wholesalers  Value 1 (Jun-2023) 
A0
B5273.12
C0
D803151.57
E0
F0
G0

In the output below , we are assuming that Jun-2023 is selected from month year slicer , so the values shown in column Value 1 (Jun-2023) is summation of TransactionCurrency01 + TransactionCurrency02 + TransactionCurrency03 where fiscal year = 2023 against respective wholesaler . For example : For wholesaler B , 758.45 + 2129.56 + 2385.11 = 5273.12 and so on .

@amitchandak 

@jay_patel Once you unpivot your data and add a few helper columns this becomes pretty simple. See attached PBIX below signature.

Measure = 
    VAR __Wholesaler = MAX('Table'[Wholesalers  ])
    VAR __FiscalYear = MAX('Table'[Fiscal_Year  ])
    VAR __Date = MAX('Table'[Date])
    VAR __Table = FILTER(ALL('Table'), [Wholesalers  ] = __Wholesaler && [Fiscal_Year  ] = __FiscalYear && [Date] <= __Date)
    VAR __Result = SUMX( __Table, [Value] )
RETURN
    __Result

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Your DAX works fine with sample data . But when TransactionCurrency10, TransactionCurrency11 and TransactionCurrency12 comes into the picture then it creates some sort of issue.

Below is the table which tells that what should be the summation of TransactionCurrency with respected month year : 

(I have total 12 column of TransactionCurrency in data)

 

Month   TransactionCurreny
AprTransctionCurrency01
MayTransctionCurrency01+TransctionCurrency02
JunTransctionCurrency01+TransctionCurrency02+TransctionCurrency03
JulTransctionCurrency01+TransctionCurrency02+TransctionCurrency03+TransctionCurrency04
AugTransctionCurrency01 to TransctionCurrency05
SepTransctionCurrency01 to TransctionCurrency06
OctTransctionCurrency01 to TransctionCurrency07
NovTransctionCurrency01 to TransctionCurrency08
DecTransctionCurrency01 to TransctionCurrency09
JanTransctionCurrency01 to TransctionCurrency10
FebTransctionCurrency01 to TransctionCurrency11
MarTransctionCurrency01 to TransctionCurrency12

 

Currently, when I am selecting Jul-2023 from slicer then the <= conditon from DAX includes data from Jan-2023 to Jul-2023 (refer below table after unpivoting and creating few calculated columns as given in PBIX file above) , but it should give data from Apr-2023 to Jul-2023 i.e from  TransctionCurrency01+TransctionCurrency02+TransctionCurrency03+TransctionCurrency04 (same for all fiscal years) :

 

Attribute  Custom  addition  year mon  date
TransctionCurrency10     10     120230101-Jan-23
TransctionCurrency10     10     120230101-Jan-23
TransctionCurrency11     11     220230201-Feb-23
TransctionCurrency11     11     220230201-Feb-23
TransctionCurrency12     12     320230301-Mar-23
TransctionCurrency12     12     320230301-Mar-23
TransctionCurrency01        1     420230401-Apr-23
TransctionCurrency01      1     420230401-Apr-23
TransctionCurrency02      2     520230501-May-23
TransctionCurrency02      2     520230501-May-23
TransctionCurrency03      3     620230601-Jun-23
TransctionCurrency03      3     620230601-Jun-23
TransctionCurrency04      4     720230701-Jul-23
TransctionCurrency04      4     720230701-Jul-23

 

Also, please do check for months Jan , Feb , Mar also as I think the above DAX won't work there. i.e if I am selecting Jan then it should sum from TransctionCurrency01 to TransctionCurrency10 (as mentioned in 1st table). 

 

@amitchandak 

@jay_patel This is a bit hacky but works. See PBIX. You really should be using a fiscal calendar for this sort of thing.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.