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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
MattiaMaetini
Helper I
Helper I

Calculate difference percentage between two rows in matrix with dynamic date on rows

Hi guys,

I need help to obtain this table (my goal is to calculate the column % difference):

 14/04/202214/04/2023% difference
Budget100000011000009%
Assegnato750000700000-7%
Impegnato30000035000014%

 

My problem is that in the original table i have data in this format:

CategoryValueDate
Budget100000014/04/2022
Assegnato75000014/04/2022
Impegnato30000014/04/2022
Budget110000014/04/2023
Assegnato70000014/04/2023
Impegnato35000014/04/2023

 

I create a data model that allow my user to select a Month (in this case April) and I can automatically filter my datatable with two set of data from April 2023 and the same date in 2022.

 

So how can I reference in a new measure to the date that will be generated by slicer when I select a month? 

 

Tks in advance,

M

4 REPLIES 4
Greg_Deckler
Super User
Super User

@MattiaMaetini Try something like:

Measure = 
  VAR __Category = MAX('Table'[Category])
  VAR __Value = MAX('Table'[Value])
  VAR __Date = MAX('Table'[Date])
  VAR __LYDate = DATE(YEAR(__Date) - 1, MONTH(__Date), DAY(__Date))
  VAR __LYValue = MAXX(FILTER('Table', [Category] = __Category && [Date] = __LYDate), [Value])
  VAR __Result = DIVIDE( __Value - __LYValue), __LYValue)
RETURN
  __Result

Follow on LinkedIn
@ 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 two questions:

1) formula for calculate date couldn't work 'cause the two dates not necessary refer to the same day: I have a table where I stored these informations (for every month i create two record, one for every year):

 

DateMonthMonth_Label
14/04/20224April
14/04/20234April

 

2) MAX(Category) could work only for one Category, I need to iterate for all values in order to obtain my goal

 

Tks

@MattiaMaetini I was guessing that you were going to have Category in some sort of visual. Is that not the case? If not, are you creating a calculated table or ??? If you are creating a calculated table, you might be able to do this with a bit of modification to the below measure like replacing the first few MAX with MAXX( __Table, [Category]), MAXX(__Table, [Value]), etc.

Caculted Table = 
  VAR __Table =
    ADDCOLUMNS(
      SUMMARIZE(
        'Table',
        [Category],
        [Date],
        "Value", SUM('Table'[Value])
      ),
      "__Percent", [Measure]
  )
RETURN
  __Table

 

 

Now that you have let me know about an additional table you can do this:

 

Measure = 
  VAR __Category = MAX('Table'[Category])
  VAR __Value = MAX('Table'[Value])
  VAR __Date = MAX('Table'[Date])
  VAR __LYDate = 
    MAXX(
      FILTER(
        'DatesTable',
        YEAR([Date]) =  YEAR(__Date) - 1 && 
        [Month] = MONTH(__Date)
      ),
      [Date]
    )
  VAR __LYValue = MAXX(FILTER('Table', [Category] = __Category && [Date] = __LYDate), [Value])
  VAR __Result = DIVIDE( __Value - __LYValue), __LYValue)
RETURN
  __Result

 

 


Follow on LinkedIn
@ 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  I have Category in my visual, but how you can see in the screenshot I have two date filtered by only one column in original table (column name is Data).

I need to refer to these two date, for each Category, and calculate percentage difference.

 

Tks,

MattiaMaetini_0-1683183835106.png

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.