Daily DAX : Day 221 PREVIOUSDAY

 The PREVIOUSDAY function in Power BI DAX (Data Analysis Expressions) returns a table containing a single column of date values representing the previous day relative to a specified date. It’s commonly used in time intelligence calculations to compare data from the current day with the previous day.

Syntax

DAX


PREVIOUSDAY(<date_column>)


    <date_column>: A column containing date values or an expression that returns a date.


Return Value

A table with a single column of dates, containing only the date immediately preceding the specified date in the context.

How It Works


    PREVIOUSDAY identifies the date in the current filter context and returns the date just before it.

    It works within a date table or a column of dates and requires a proper date table with continuous dates for accurate results.

    The function is often used in measures to calculate metrics like day-over-day changes, such as sales, revenue, or other KPIs.


Use Case

Scenario: You want to compare today’s sales with yesterday’s sales to calculate the day-over-day sales change.

Example:

Assume you have a table Sales with columns Date and SalesAmount, and a related DateTable marked as a date table in Power BI.


    Create a measure for previous day’s sales:

    DAX


    PreviousDaySales = 

    CALCULATE(

        SUM(Sales[SalesAmount]),

        PREVIOUSDAY('DateTable'[Date])

    )


    Create a measure for day-over-day change:

    DAX


    DayOverDayChange = 

    SUM(Sales[SalesAmount]) - [PreviousDaySales]


    Use case in action:

        If the current filter context is May 22, 2025, PREVIOUSDAY('DateTable'[Date]) returns a table with the date May 21, 2025.

        The PreviousDaySales measure calculates the total sales for May 21, 2025.

        The DayOverDayChange measure computes the difference between sales on May 22 and May 21.


Visualization:

You can display these measures in a Power BI report (e.g., a table or card visual) to show daily sales trends or highlight significant changes.

Key Notes


    Date Table Requirement: Ensure your model has a proper date table with consecutive dates, marked as the date table in Power BI, to avoid gaps or incorrect results.

    Filter Context: The function respects the filter context, so it’s affected by slicers, filters, or other DAX calculations.

    Single Date Output: If no previous day exists (e.g., the earliest date in the table), the function returns an empty table.

    Common Applications: Used for daily comparisons, trend analysis, or monitoring KPIs like website traffic, inventory levels, or financial metrics.


Example in a Report

Suppose you’re analyzing website traffic:


    Measure:

    DAX


    PreviousDayVisitors = 

    CALCULATE(

        COUNT(Website[VisitorID]),

        PREVIOUSDAY('DateTable'[Date])

    )


    Output: If today is May 22, 2025, and you had 1,000 visitors on May 21, 2025, this measure returns 1,000. You can then compare it with today’s visitor count.


This function is particularly useful for time-based analytics in business intelligence dashboards.

Comments

Popular posts from this blog

Daily DAX : Day 65 INFO.TABLEPERMISSIONS

Daily DAX : Day 55 PV