Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

YTD Change

Hi,

I have a table like below:-

Current BalanceDate              Deal  Price  Value
3412/14/2022A703
2312/14/2022A602
2312/14/2022A5058
2312/14/2022B7010
9812/14/2022B603
6512/14/2022B506
4312/14/2022C708
2212/14/2022C609
4412/14/2022C504

 

I want to add a calculated column YTD change which is calculated as follows:-

 

  • For Deal A for Price 70 >> Filter the table for Deal A and Price 70 and then Value for Latest Date minus Value for First Date in the year
  • For Deal A for Price 60 >> Filter the table for Deal A and Price 60 and then Value for Latest Date minus Value for First Date in the year
  • For Deal A for Price 50 >> Filter the table for Deal A and Price 50 and then Value for Latest Date minus Value for First Date in the year

and so on........

 

Need help with the DAX formula to create this column please

  • Anonymous This is essentially MTBF. Try this:

    Column = 
      VAR __Deal = [Deal]
      VAR __Price = [Price]
      VAR __FirstDate = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price), [Date])
      VAR __LastDate = MAXX(FILTER('Table', [Deal] = __Deal && [Price] = __Price), [Date])
      VAR __FirstValue = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price && [Date] = __FirstDate), [Value])
      VAR __LastValue = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price && [Date] = __LastDate), [Value])
      VAR __Result = __LastValue - __FirstValue
    RETURN
      __Result
  • Anonymous  in this case,

    first create a calc column in the orginal table

    Deal_Date = 'Table'[Deal]&"|"&'Table'[Date]

     

    then you can create another calc table from the main table like below

     

     then create below colums in the new table

    Deal_Min_Date = Min_Max_value_2[Deal] & "|" & Min_Max_value_2[Min Date]
    Deal_Max_Date = Min_Max_value_2[Deal] & "|" & Min_Max_value_2[max Date]
    Min_Date_Value = LOOKUPVALUE('Table'[Value],'Table'[Deal_Date],Min_Max_value_2[Deal_Min_Date])
    Max_Date_Value = LOOKUPVALUE('Table'[Value],'Table'[Deal_Date],Min_Max_value_2[Deal_Max_Date])
    Max-Min = Min_Max_value_2[Max_Date_Value]-Min_Max_value_2[Min_Date_Value]
     
    please note that you need all above columns, you can combine them in one dax column to have a clean view. just for illustration purpose, i have added all columns
     
    also attached pbix file.

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Anonymous This is essentially MTBF. Try this:

    Column = 
      VAR __Deal = [Deal]
      VAR __Price = [Price]
      VAR __FirstDate = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price), [Date])
      VAR __LastDate = MAXX(FILTER('Table', [Deal] = __Deal && [Price] = __Price), [Date])
      VAR __FirstValue = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price && [Date] = __FirstDate), [Value])
      VAR __LastValue = MINX(FILTER('Table', [Deal] = __Deal && [Price] = __Price && [Date] = __LastDate), [Value])
      VAR __Result = __LastValue - __FirstValue
    RETURN
      __Result
    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks, this was perfect!

    • Anonymous's avatar
      Anonymous
      Not applicable

      Greg_Deckler , one question:

      To calculate variable _First Value, you are using a min function on the filtered table.

      If I have data for both 2021 and 2022, would not this variable pick 2021 value instead of first date of 2022 to calculate YTD?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, I have a table like below:

       

      DateSectorValue
      1/7/2015A44
      1/14/2015A56
      1/21/2025A7
      1/28/2015A8
      1/7/2015B3
      1/14/2015B4
      1/21/2025B67
      1/28/2015B54
      1/7/2015C66
      1/14/2015C43
      1/21/2025C2
      1/28/2015C1

       

      I want to create a summary table where I show the WoW change and MoM change from the latest date for every sector. I am having a hard time writing the correct DAX code to calculate both of these metrics. Please help.

       

      Thanks,

       

  • negi007's avatar
    negi007
    Community Champion

    Anonymous  in this case,

    first create a calc column in the orginal table

    Deal_Date = 'Table'[Deal]&"|"&'Table'[Date]

     

    then you can create another calc table from the main table like below

     

     then create below colums in the new table

    Deal_Min_Date = Min_Max_value_2[Deal] & "|" & Min_Max_value_2[Min Date]
    Deal_Max_Date = Min_Max_value_2[Deal] & "|" & Min_Max_value_2[max Date]
    Min_Date_Value = LOOKUPVALUE('Table'[Value],'Table'[Deal_Date],Min_Max_value_2[Deal_Min_Date])
    Max_Date_Value = LOOKUPVALUE('Table'[Value],'Table'[Deal_Date],Min_Max_value_2[Deal_Max_Date])
    Max-Min = Min_Max_value_2[Max_Date_Value]-Min_Max_value_2[Min_Date_Value]
     
    please note that you need all above columns, you can combine them in one dax column to have a clean view. just for illustration purpose, i have added all columns
     
    also attached pbix file.