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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
felixzhao123
Regular Visitor

Calculate the Change of Values by Dates

Hi,

I have a table in my dataset that contains the product stock. What i want to achieve is to calculate the stock value change by product category based on the date range selected by user with date filter visual. The difference is between the stock value by category in the most recent date selected and oldest date selected.

The table looks like this:

Screenshot 2024-04-17 120437.png

I tried with SUMMARIZECOLUMNS and FILTER but not returning what I expected.

Thanks in advance,

 

Felix

 

1 ACCEPTED SOLUTION

Hi,@felixzhao123 

Here's my explanation of how I use my functions:

1.It works by using category as the grouping condition, then the ALLSELECTED() function retains this filtering condition, and finally MAXX() counts the maximum value within this group.

2.To make it easier for you to understand, I have created the following table:

vlinyulumsft_0-1714705485570.png

There are two more measure:

Measure 1 = MAX('Table'[Column1])
Measure 2= CALCULATE(SUM('Table'[Column2]),FILTER(ALLSELECTED('Table'),'Table'[Column1]=MAX('Table'[Column1])))

Here is the output:

vlinyulumsft_0-1714705798923.png

 

You can clearly understand in measure1 that the max function returns either A or B in that column, which is the grouping condition mentioned in our previous formula.

Measure2, on the other hand, is a simple use case to add up the values of the same group.

3.In short, the purpose of this formula is to find the rows with the largest dates in each category in the user-selected context, and then calculate the maximum value of the date field for those rows. This approach is useful when analyzing time series data or when you need to group comparisons based on categories.

4.In the DAX function, the ALLSELECTED() function serves to remove the context filters for columns and rows from the current query while retaining all other context filters or explicit filters. This function can be used to get visual totals in a query. Unlike the ALL() function, the ALL function returns all rows in the base table or all base columns with different values, ignoring any filters that may have been set on the table or the specified column.

5.Here is the introductory document, hopefully this will help you understand what this formula does:

MAX function (DAX) - DAX | Microsoft Learn
ALLSELECTED function (DAX) - DAX | Microsoft Learn

MAXX function (DAX) - DAX | Microsoft Learn

Best Regards,

Leroy Lu

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

3 REPLIES 3
v-linyulu-msft
Community Support
Community Support

Hi @felixzhao123 ,

 

Regarding the issue you raised, my solution is as follow:

1. First of all I have created a table based on the table you have provided, the table name is “report' and the structure of the table is shown below:

vlinyulumsft_0-1713347096033.png

 

2. As I understand it, your filtering criteria are only time and category, so I created this measure below:

First I got the maximum and minimum values of the dates respectively, then I used the max function to determine the values of the same group, and the date is the maximum or minimum value, and subtracted the values of the inv columns to come up with the final answer.

Measure = 
var _mindate=
MINX(FILTER(ALLSELECTED('Table'),'Table'[castgory]=MAX('Table'[castgory])),[Date])
var _maxdate=
MAXX(FILTER(ALLSELECTED('Table'),'Table'[castgory]=MAX('Table'[castgory])),[Date])
var _minvalue=
SUMX(
    FILTER(ALL('Table'),'Table'[castgory]=MAX('Table'[castgory])&&'Table'[Date]=_mindate),[inv¥])
var _maxvalue=
SUMX(
    FILTER(ALL('Table'),'Table'[castgory]=MAX('Table'[castgory])&&'Table'[Date]=_maxdate),[inv¥])
return
_minvalue - _maxvalue

3. Here's my final result, which I hope meets your requirements.

vlinyulumsft_1-1713347147700.png

vlinyulumsft_2-1713347147702.png

 

Best Regards,

Leroy Lu

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

 

Thanks for your help! Would you please help me understand why in this statement:

MAXX(FILTER(ALLSELECTED('Table'),'Table'[castgory]=MAX('Table'[castgory])),[Date])

we need 'Table'[castogry]=max('Table'[castgory])?

Thanks!

 

Felix

Hi,@felixzhao123 

Here's my explanation of how I use my functions:

1.It works by using category as the grouping condition, then the ALLSELECTED() function retains this filtering condition, and finally MAXX() counts the maximum value within this group.

2.To make it easier for you to understand, I have created the following table:

vlinyulumsft_0-1714705485570.png

There are two more measure:

Measure 1 = MAX('Table'[Column1])
Measure 2= CALCULATE(SUM('Table'[Column2]),FILTER(ALLSELECTED('Table'),'Table'[Column1]=MAX('Table'[Column1])))

Here is the output:

vlinyulumsft_0-1714705798923.png

 

You can clearly understand in measure1 that the max function returns either A or B in that column, which is the grouping condition mentioned in our previous formula.

Measure2, on the other hand, is a simple use case to add up the values of the same group.

3.In short, the purpose of this formula is to find the rows with the largest dates in each category in the user-selected context, and then calculate the maximum value of the date field for those rows. This approach is useful when analyzing time series data or when you need to group comparisons based on categories.

4.In the DAX function, the ALLSELECTED() function serves to remove the context filters for columns and rows from the current query while retaining all other context filters or explicit filters. This function can be used to get visual totals in a query. Unlike the ALL() function, the ALL function returns all rows in the base table or all base columns with different values, ignoring any filters that may have been set on the table or the specified column.

5.Here is the introductory document, hopefully this will help you understand what this formula does:

MAX function (DAX) - DAX | Microsoft Learn
ALLSELECTED function (DAX) - DAX | Microsoft Learn

MAXX function (DAX) - DAX | Microsoft Learn

Best Regards,

Leroy Lu

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

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.