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
Anonymous
Not applicable

DAX - Same period comparsion

Hi guys,

 

I ran into a puzzle I can't solve with my DAX knowledge.

 

I will try to shortly explain what I'm trying to achieve and then show you what I was able to do so far.

 

Let's assume my current fact in the data warehouse looks like this:

StoreIDDateIDQty
11.1.201910
11.2.201910
11.3.201910
21.2.201915
21.3.201915

 

As you can see, the 1st store began selling their products in Jan-2019, however, the 2nd store began in Feb-2019.
Because I only want to compare the same periods, I need to filter this fact this way:

StoreIDDateIDQty
11.1.201910
11.2.201910
11.3.201910
21.2.201915
21.3.201915

 

If I put this into SQL language, this is basically everything I want to do:

 

DECLARE @MaxDate DATE = '';
SELECT
	@MaxDate = MAX(MinDateID)
FROM
	(
		SELECT
			MIN(DateID) AS MinDateID,
			StoreID
		FROM
			FactSalesInvoice
		WHERE 
			StoreID IN (1, 2)
		GROUP BY
			StoreID
	)

SELECT
	*
FROM
	FactSalesInvoice
WHERE
	DateID > @MaxDate
AND StoreID IN (1, 2)

The users would then be able to compare store data for the periods that all chosen stores were already established.

So, I've tried creating a Min Date per Store table like this:

SUMMARIZE('Sales Invoice','Sales Invoice'[StoreID], "MinDate", MIN('Sales Invoice'[DateID]))


And then - many variations of it, but none of them worked the way I wanted it to - a measure:

SamePeriodSale = 
VAR MaxSalesInvoiceDate = MAX(MinDateSalesInvoice[MinDate])
RETURN
CALCULATE (
    'Sales Invoice'[Sales Invoice Value],
    FILTER('Date', 'Date'[DateID] >= MaxSalesInvoiceDate)
)

Basically, the one thing I really can't understand, is how to get a variable that would calculate 1.2.2019 for all of the rows in the table. Right now, I'm able to get 1.1.2019 for the 1st store, and 1.2.2019 for the 2nd store.

What is the way to get on top of this thing? I'm really not familiar with DAX yet, but I'm hoping someone can help me. 

Thanks in advance.

4 REPLIES 4
v-lili6-msft
Community Support
Community Support

hi, @Anonymous 

First, you should know that calculated column and calculate table can't be affected by any slicer. you could create a measure instead of column.
Notice:
1. Calculation column/table not support dynamic changed based on filter or slicer.
2. Measure can be affected by filter/slicer, so you can use it to get dynamic summary result.
here is reference:
https://community.powerbi.com/t5/Desktop/Different-between-calculated-column-and-measure-Using-SUM/t...
https://www.sqlbi.com/articles/calculated-columns-and-measures-in-dax/

Second, based on my research, you could create these two measures

Date of the public = CALCULATE(CONCATENATEX(VALUES('Table'[DateID]),[DateID],,CALCULATE(MAX('Table'[DateID])),ASC),FILTER(ALLSELECTED('Table'),'Table'[DateID]<=MAX('Table'[DateID])))
filterstatus = IF(CALCULATE( COUNTAX('Table',[Date of the public]),ALLSELECTED('Table'[StoreID]))=CALCULATE(DISTINCTCOUNT('Table'[StoreID]),ALLSELECTED('Table')),1,2)

Then drag measure[filterstatus] into visual level filter of table visual and set filter is "1"

3.JPG

Result:

4.JPG

 

and here is pbix file, please try it.

 

Best Regards,

Lin

 

 

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Hi,

@PattemManohar - your solution does not take filters into consideration. If I selected one store ID, it still gets MAX from all the stores.


@v-lili6-msft,

 

I've tested your solution and it works the way I'd want it. But if I test it on our data, it immediately runs out of memory even on a small dataset (1000s of records), but in production, we're gonna have billions of records. 


So this is a no-go. Is there a better way?

 
And also, as you've stated, the measures aren't affected by slicers.

hi, @Anonymous 

And also, as you've stated, the measures aren't affected by slicers.

First, you have a wrong understanding of it.

1. Calculation column/table not support dynamic changed based on filter or slicer.
2. Measure can be affected by filter/slicer, so you can use it to get dynamic summary result.

Second, For the logic of this measure will iterate over each row of data multiple times and then filter data in the memory, So it will take up a lot of memory. Maybe we need to optimize the formula.

could you share us more sample data (small dataset (1000s of records)) for us as a test, You can upload it to OneDrive and post the link here. Do mask sensitive data before uploading

 

 

Best Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
PattemManohar
Community Champion
Community Champion

@Anonymous  Please try this as a New Table.

 

Test232Out = 
VAR _MinDate = MAXX(SELECTCOLUMNS(SUMMARIZE(Test232Filter,Test232Filter[StoreID],"MinVal",MIN(Test232Filter[DateID])),"Minval",[MinVal]),[Minval])
RETURN FILTER(Test232Filter,Test232Filter[DateID]>=_MinDate)

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




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.