Automationscribe.com
  • Home
  • AI Scribe
  • AI Tools
  • Artificial Intelligence
  • Contact Us
No Result
View All Result
Automation Scribe
  • Home
  • AI Scribe
  • AI Tools
  • Artificial Intelligence
  • Contact Us
No Result
View All Result
Automationscribe.com
No Result
View All Result

Underneath the Hood: How DAX Works with Filters

admin by admin
October 1, 2025
in Artificial Intelligence
0
Underneath the Hood: How DAX Works with Filters
399
SHARES
2.3k
VIEWS
Share on FacebookShare on Twitter


Let’s begin with a easy desk:

Determine 1 – The easy desk to start out with (Determine by the Creator)

Every row within the matrix visible reveals the full on-line gross sales for every month.
Up to now, so good.

The interpretation is that we see the full on-line gross sales filtered by month.

However this isn’t your entire fact.

Let’s check out the information mannequin:

Determine 2 – The a part of the information mannequin with the Date desk and the very fact desk (Determine by the Creator)

While you look fastidiously, you see that the connection is constructed between the 2 date columns.

There isn’t any relationship to the month column.

After we take this route, the interpretation above shouldn’t be fully correct.

The whole interpretation must be: Every row reveals the full of on-line gross sales filtered by the date desk. The rows of the Date desk are grouped by the months. Every row reveals the full gross sales for all days for every month.

We’re a step nearer to understanding DAX normally, and time intelligence features specifically, after we notice this element.

Let’s go one step additional.

YTD and the Fundamental question

Now, let’s add a YTD Measure to look at what occurs:

Determine 3 – The YTD Measure and the outcomes are in the identical desk as earlier than (Determine by the Creator)

The Measure is nothing particular, and the outcomes are simple to grasp.

Now, let’s check out what exactly the DATESYTD() perform does.

The reason from dax.information says: “Returns a set of dates within the yr as much as the final date seen within the filter context”.

What does this imply precisely?

To dive into this query, let’s write a DAX question first, to get the record of dates in June 2024, as is completed within the visualization above:

DEFINE
    VAR YearFilter = TREATAS({ 2024 }, 'Date'[Year])
    VAR MonthFilter = TREATAS({ 6 }, 'Date'[Month])
    
    
EVALUATE
    SUMMARIZECOLUMNS('Date'[Date]
                        ,YearFilter
                        ,MonthFilter
                        )

The result’s a listing of 30 days for June:

Determine 4 – Foundation Question to get all days for June 2024 (Determine by the Creator)

That is the filter utilized to the row for June 2024 within the Matrix proven above.

What’s the consequence after we apply the DATESYTD() perform to the consequence?

Right here is the question:

DEFINE
    VAR YearFilter = TREATAS({ 2024 }, 'Date'[Year])
    VAR MonthFilter = TREATAS({ 6 }, 'Date'[Month])
    
    VAR BasisDates = CALCULATETABLE(
                            SUMMARIZECOLUMNS('Date'[Date]
                                        ,YearFilter
                                        ,MonthFilter
                                        )
                                    )
    
    VAR YTDDates = DATESYTD(TREATAS(BasisDates, 'Date'[Date])
                                    )
                                    
EVALUATE
    YTDDates

And right here, the consequence:

Determine 5 – Checklist of dates beginning on the primary of January as much as the final day of June 2024 (Determine by the Creator)

It’s a listing of 182 rows, containing all dates ranging from the start of the yr as much as the final day of June 2024.

That is the definition of YTD.

After we take a look at the next Measure:

On-line Gross sales (YTD) =
VAR YTDDates = DATESYTD('Date'[Date])

RETURN
    CALCULATE([Sum Online Sales]
                ,YTDDates
                )

We notice that the variable YTDDates is “solely” a listing of dates utilized as a filter to the CALCULATE() perform.

That is the important thing to all Time intelligence features.

Return one yr – some examples

What occurs when making use of one other perform to the consequence?

For instance, SAMEPERIODLASTYEAR()?

To reply this query, I take advantage of the next DAX question:

DEFINE
    VAR YearFilter = TREATAS({ 2024 }, 'Date'[Year])
    VAR MonthFilter = TREATAS({ 6 }, 'Date'[Month])
    
    VAR BasisDates = CALCULATETABLE(
                            SUMMARIZECOLUMNS('Date'[Date]
                                        ,YearFilter
                                        ,MonthFilter
                                        )
                                    )
    
    VAR YTDDates = DATESYTD(TREATAS(BasisDates, 'Date'[Date])
                                    )
                                    
    VAR YTDDatesPY = SAMEPERIODLASTYEAR(YTDDates)
                                    
EVALUATE
    YTDDatesPY

I deliberately separated the calling of SAMEPERIODLASTYEAR() from DATESYTD() to make it simpler to learn. It could have been attainable to nest DATESYTD() into SAMEPERIODLASTYEAR().

This time, we now have 181 rows, as 2024 was a bissextile year.
And the dates are shifted again by one yr:

Determine 6 – The results of the question after making use of SAMEPERIODLASTYEAR() (Determine by the Creator)

So, once more, after we apply a Time Intelligence perform to a measure, the perform, for instance, DATESYTD(), returns a listing of dates.
Please word: When making use of the filter, any present filters on the date desk are eliminated.

Customized logic

Now, let’s use this information on customized time intelligence logic.

First, let’s barely change the filter for the yr and month:

DEFINE
    VAR YearMonthFilter = TREATAS({ 202406  }, 'Date'[MonthKey])
    
EVALUATE
    SUMMARIZECOLUMNS('Date'[Date]
                        , YearMonthFilter
                        )

The results of this question is similar as in the beginning of this text.

This time, I set the filter with a numerical worth on the [MonthKey] column.

How can I’m going again to the earlier yr?

Should you suppose mathematically, it’s simply by subtracting 100:

202406 – 100 = 202306

Let’s strive it:

Determine 7 – The results of the question after deducting 100 from the [MonthKey] column (Determine by the Creator)

You can too do that with different numeric codecs.

While you take a fiscal yr, for instance, like this: 2425 (For the Fiscal 12 months 24/25)

You’ll be able to deduce 101 to get the earlier fiscal yr: 2425 – 101 = 2324

One other instance of a customized time intelligence logic is a operating common, the place for every day, we calculate the common worth over the previous 10 days:

Determine 8 – The Code and the outcomes of the Measure for a shifting common over the earlier ten days (Determine by the Creator)

Because the content material of the variable DateRange is once more a listing of dates, I can apply the SAMEPERIODLASTYEAR() perform to it, and get the consequence I want:

DEFINE
    VAR YearFilter = TREATAS({ 2024 }, 'Date'[Year])
    VAR MonthFilter = TREATAS({ 6 }, 'Date'[Month])
    
    // 1. Get the primary and final Date for the present Filter Context
    VAR MaxDate = CALCULATE(MAX( 'Date'[Date] )
                            ,YearFilter
                            ,MonthFilter
                            )
    
    VAR MinDate =
        CALCULATE(
            DATEADD( 'Date'[Date], - 10, DAY )
            ,'Date'[Date] = MaxDate
            )
    
    // 2. Generate the Date vary wanted for the Shifting common (4 months)
    VAR  DateRange =
     CALCULATETABLE(
        DATESBETWEEN( 'Date'[Date]
            ,MinDate
            ,MaxDate
        )
    )
    
EVALUATE
    SAMEPERIODLASTYEAR( DateRange )

And that is the consequence:

Determine 9 – The consequence for the shifting common for the earlier yr (Determine by the Creator)

This logic returns 11 rows, because it consists of the final day of the month. Relying on the required consequence, we have to regulate the way in which we calculate the primary and final dates of the date record (the filter utilized to the measure).

After all, it is a repetition of what I confirmed above. Nonetheless, it demonstrates that the identical strategy could be utilized to numerous eventualities.

As quickly as you perceive this, your work with time intelligence features and different features that settle for tables of values as enter will change into a lot simpler to grasp and grasp.

Conclusion

Whereas I used DAX Studio for the queries, you should utilize the identical queries within the DAX Question instrument inside Energy BI Desktop.

I deliberately used these queries to display that we work with tables on a regular basis in DAX, though we could not at all times pay attention to it.

However it’s an vital element that helps us on our approach to understanding DAX.

Though among the DAX code proven right here could also be out of date with the arrival of the brand new calendar-based time intelligence characteristic in Energy BI, the ideas defined right here stay legitimate. The features, similar to DATESYTD() or SAMEPERIODLASTYEAR(), nonetheless exist and work in the identical manner as earlier than. In the interim, nothing will change on this aspect, because the ideas described listed below are nonetheless legitimate.

References

Like in my earlier articles, I take advantage of the Contoso pattern dataset. You’ll be able to obtain the ContosoRetailDW Dataset without spending a dime from Microsoft right here.

The Contoso Information could be freely used below the MIT License, as described on this doc. I modified the dataset to shift the information to up to date dates.

Tags: DAXfiltersHoodWorks
Previous Post

Modernize fraud prevention: GraphStorm v0.5 for real-time inference

Next Post

How Hapag-Lloyd improved schedule reliability with ML-powered vessel schedule predictions utilizing Amazon SageMaker

Next Post
How Hapag-Lloyd improved schedule reliability with ML-powered vessel schedule predictions utilizing Amazon SageMaker

How Hapag-Lloyd improved schedule reliability with ML-powered vessel schedule predictions utilizing Amazon SageMaker

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Popular News

  • How Aviva constructed a scalable, safe, and dependable MLOps platform utilizing Amazon SageMaker

    How Aviva constructed a scalable, safe, and dependable MLOps platform utilizing Amazon SageMaker

    402 shares
    Share 161 Tweet 101
  • Diffusion Mannequin from Scratch in Pytorch | by Nicholas DiSalvo | Jul, 2024

    402 shares
    Share 161 Tweet 101
  • Unlocking Japanese LLMs with AWS Trainium: Innovators Showcase from the AWS LLM Growth Assist Program

    402 shares
    Share 161 Tweet 101
  • Streamlit fairly styled dataframes half 1: utilizing the pandas Styler

    401 shares
    Share 160 Tweet 100
  • Autonomous mortgage processing utilizing Amazon Bedrock Knowledge Automation and Amazon Bedrock Brokers

    401 shares
    Share 160 Tweet 100

About Us

Automation Scribe is your go-to site for easy-to-understand Artificial Intelligence (AI) articles. Discover insights on AI tools, AI Scribe, and more. Stay updated with the latest advancements in AI technology. Dive into the world of automation with simplified explanations and informative content. Visit us today!

Category

  • AI Scribe
  • AI Tools
  • Artificial Intelligence

Recent Posts

  • Automate Amazon QuickSight knowledge tales creation with agentic AI utilizing Amazon Nova Act
  • This Puzzle Exhibits Simply How Far LLMs Have Progressed in a Little Over a Yr
  • Accountable AI: How PowerSchool safeguards tens of millions of scholars with AI-powered content material filtering utilizing Amazon SageMaker AI
  • Home
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions

© 2024 automationscribe.com. All rights reserved.

No Result
View All Result
  • Home
  • AI Scribe
  • AI Tools
  • Artificial Intelligence
  • Contact Us

© 2024 automationscribe.com. All rights reserved.