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

How to calculate YTD of cost based on Period filter?

Hey Guys,
 
I am trying to have a YTD Calculation for the metric cost as shown in the visualization. The calculation will be done based on the Period selected by the user. Let's say if the user selects P03, then in both the respective year columns I should be getting sum of cost for P01+P02+P03.
The problem with the measure that I have written is that It is unable to distinguish between the year. So I am not getting any values in my visualization.
 
Here's the dummy data (Level-A-B-C-D):
 
ABCDFiscal PeriodFiscal YearPeriodCost
xxyyzzkk2018-P012018P01100
aabbccdd2018-P012018P01100
aabbccdd2018-P022018P02200
aabbccdd2018-P032018P03300
aayyzzkk2018-P042018P04400
xxbacada2018-P052018P05500
xxbccbdb2018-P062018P06600
xxbdcbdc2018-P072018P07700
xxbbcbdd2018-P082018P08800
xzbsccde2018-P092018P09900
xzbacddf2018-P102018P101000
xzblcddg2018-P112018P111100
xzbmcadl2018-P122018P121200
xxyyzzkk2019-P012019P011300
aabbccdd2019-P022019P021400
aabbccdd2019-P032019P031500
aabbccdd2019-P042019P041600
aayyzzkk2019-P052019P051700
xxbacada2019-P062019P061800
xxbccbdb2019-P072019P071900
xxbdcbdc2019-P082019P082000
xxbbcbdd2019-P092019P092100
xzbsccde2019-P102019P102200
xzbacddf2019-P112019P112300
xzblcddg2019-P122019P122400
 
 
Below is the measure that I have written-
 
YTD_calc = 
 
VAR prd_selection = SELECTEDVALUE(Dummy_Table[fiscal_period]) // I would essentially need only Period here
Var yr = LEFT(prd_selection,4)
 Var prd = RIGHT(prd_selection,3)
 
 VAR prior__prd = (VALUE(yr)-1) & "-" & prd
 
return 
 
CALCULATE( SUM(Dummy_Table[actual_tonnage_net_wt]), FILTER(ALLEXCEPT(Dummy_Table, Dummy_Table[A], Dummy_Table[B], Dummy_Table[C], Dummy_Table[D]), 
Dummy_Table[fiscal_period] <= prd_selection && Dummy_Table[fiscal_year] = VAlUE(yr))
,
 FILTER(ALLEXCEPT(Dummy_Table,Dummy_Table[A],Dummy_Table[B,Dummy_Table[C],Dummy_Table[D]), Dummy_Table[period]<= prd
 && Dummy_Table[fiscal_year]= VALUE((yr)-1)))


 
 This is not working as this measure is unable to classify between the year columns! Any help would be appreciated. Thank you 🙂
@amitchandak  @Greg_Deckler @parry2k  @Cmcmahan 



1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @Apoorv_Lunawat ,

 

I create a sample based on your description. It can get sum of cost when we use slicer. For example, it will show sum of P01+P02+P03 while selecting P03.

Fiscal Period = VALUES('Table'[Fiscal Period])
Measure =
VAR prd_selection =
    SELECTEDVALUE ( 'Fiscal Period'[Fiscal Period] )
VAR yr =
    VALUE ( LEFT ( prd_selection, 4 ) )
VAR prd =
    RIGHT ( prd_selection, 3 )
VAR result =
    CALCULATE (
        SUM ( 'Table'[Cost] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Fiscal Year] = yr && 'Table'[Period] <= prd )
    )
RETURN
    IF (
        HASONEVALUE ( 'Fiscal Period'[Fiscal Period] ),
        result,
        SUM ( 'Table'[Cost] )
    )

 1.PNG

 

For more details, please see the attachment.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-xuding-msft
Community Support
Community Support

Hi @Apoorv_Lunawat ,

 

I create a sample based on your description. It can get sum of cost when we use slicer. For example, it will show sum of P01+P02+P03 while selecting P03.

Fiscal Period = VALUES('Table'[Fiscal Period])
Measure =
VAR prd_selection =
    SELECTEDVALUE ( 'Fiscal Period'[Fiscal Period] )
VAR yr =
    VALUE ( LEFT ( prd_selection, 4 ) )
VAR prd =
    RIGHT ( prd_selection, 3 )
VAR result =
    CALCULATE (
        SUM ( 'Table'[Cost] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Fiscal Year] = yr && 'Table'[Period] <= prd )
    )
RETURN
    IF (
        HASONEVALUE ( 'Fiscal Period'[Fiscal Period] ),
        result,
        SUM ( 'Table'[Cost] )
    )

 1.PNG

 

For more details, please see the attachment.

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

@Apoorv_Lunawat , Move all you period related stuff in a new table, if not date it can period key that can join

 

New columns in Date/period table

Period Year = [Fiscal Year] & [Period]
Period Rank = RANKX(ALL('Date'),'Date'[Period Year],,DESC,Dense)
period No = right([Period],2)

 

Measure

 

This Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Period Rank]=max('Date'[Period Rank])))
Last Period = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Period Rank]=max('Date'[Period Rank])-1))

Period Till Date = CALCULATE(sum('order'[Qty]), FILTER(ALL('Date'),'Date'[Fiscal Year]=max('Date'[Fiscal Year]) && 'Date'[period No] <=max('Date'[period No])))

 

Very similar or How I deal with the week here

 

https://community.powerbi.com/t5/Community-Blog/Week-Is-Not-So-Weak-WTD-Last-WTD-and-This-Week-vs-La...

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.