Here are the DAX measures to answer your two questions, considering the slicer for the time frame:
Assumptions:
- You have a
Date table in your Power BI model (if not, create one using Calendar function or auto-date/time feature) to support date-related filtering and calculations.
- The slicer for the time frame is based on this
Date table.
- The
Date column in your table is related to the Date table (if not, establish a relationship).
1. Number of days without promo sales for each Product and Payer
Measure: Days Without Promo
Days Without Promo =
-- Get the selected date range from the slicer
VAR SelectedPeriod = CALCULATETABLE(VALUES('Date'[Date]), ALLSELECTED('Date'))
-- Summarize promo periods for each Payer and Product within the selected range
VAR PromoPeriods =
SUMMARIZE(
FILTER(
'Table',
'Table'[Date] IN SelectedPeriod
),
'Table'[Payer],
'Table'[Product],
"MinDate", MIN('Table'[Start Date]), -- Earliest promo start date
"MaxDate", MAX('Table'[Final Date]) -- Latest promo end date
)
-- Count dates not within any promo period for each Payer and Product
VAR DaysWithoutPromo =
CALCULATE(
DISTINCTCOUNT('Date'[Date]),
FILTER(
SelectedPeriod,
NOT ('Date'[Date] IN
CALCULATE(
SUMMARIZE(
'PromoPeriods',
'PromoPeriods'[Payer],
'PromoPeriods'[Product],
'Date'[Date],
FILTER(
ALL('Date'),
'Date'[Date] >= 'PromoPeriods'[MinDate]
&& 'Date'[Date] <= 'PromoPeriods'[MaxDate]
)
),
'PromoPeriods'[Payer],
'PromoPeriods'[Product],
'Date'[Date]
)
)
)
)
-- Return total days without promo for each Payer and Product combination
RETURN
SUMX(
SUMMARIZE(
'Table',
'Table'[Payer],
'Table'[Product]
),
IF(
ISBLANK(DaysWithoutPromo),
0,
DaysWithoutPromo
)
)
How it works:
SelectedPeriod: Gets the selected dates from the slicer.
PromoPeriods: Summarizes the promo periods for each Payer and Product within the selected period.
DaysWithoutPromo: Counts the distinct dates within the selected period that do not fall within any promo period for each Payer and Product.
- The final measure returns the sum of days without promo for each Payer and Product combination.
2. Starting and Ending dates for periods without promo for each Product and Payer
To achieve this, we'll create two measures: one for the starting dates and one for the ending dates. We'll also create a table visual to display these dates.
Measure: Non-Promo Period Start Dates
Non-Promo Period Start Dates =
-- Get the selected date range from the slicer
VAR SelectedPeriod = CALCULATETABLE(VALUES('Date'[Date]), ALLSELECTED('Date'))
-- Summarize promo periods for each Payer and Product within the selected range
VAR PromoPeriods =
SUMMARIZE(
FILTER(
'Table',
'Table'[Date] IN SelectedPeriod
),
'Table'[Payer],
'Table'[Product],
"MinDate", MIN('Table'[Start Date]), -- Earliest promo start date
"MaxDate", MAX('Table'[Final Date]) -- Latest promo end date
)
-- Find the first date that is not within any promo period
VAR NonPromoStartDates =
CALCULATE(
MIN('Date'[Date]),
FILTER(
SelectedPeriod,
NOT (EXISTS(
FILTER(
'PromoPeriods',
VAR CurrentDate = 'Date'[Date]
RETURN
-- Check if date falls within a promo period
(CurrentDate >= 'PromoPeriods'[MinDate]
&& CurrentDate <= 'PromoPeriods'[MaxDate])
-- Check if there's a promo period immediately before this date
|| (NOT ISBLANK(
CALCULATE(
MIN('Date'[Date]),
FILTER(
ALL('Date'),
'Date'[Date] < CurrentDate
&& 'Date'[Date] >= 'PromoPeriods'[MinDate]
)
)
))
)
))
)
)
-- Return the start date if it exists, otherwise return blank
RETURN
IF(
ISBLANK(NonPromoStartDates),
BLANK(),
NonPromoStartDates
)
Measure: Non-Promo Period End Dates
Non-Promo Period End Dates =
-- Get the selected date range from the slicer
VAR SelectedPeriod = CALCULATETABLE(VALUES('Date'[Date]), ALLSELECTED('Date'))
-- Summarize promo periods for each Payer and Product within the selected range
VAR PromoPeriods =
SUMMARIZE(
FILTER(
'Table',
'Table'[Date] IN SelectedPeriod
),
'Table'[Payer],
'Table'[Product],
"MinDate", MIN('Table'[Start Date]), -- Earliest promo start date
"MaxDate", MAX('Table'[Final Date]) -- Latest promo end date
)
-- Find the last date that is not within any promo period
VAR NonPromoEndDates =
CALCULATE(
MAX('Date'[Date]),
FILTER(
SelectedPeriod,
NOT (EXISTS(
FILTER(
'PromoPeriods',
VAR CurrentDate = 'Date'[Date]
RETURN
-- Check if date falls within a promo period
(CurrentDate >= 'PromoPeriods'[MinDate]
&& CurrentDate <= 'PromoPeriods'[MaxDate])
-- Check if there's a promo period immediately after this date
|| (NOT ISBLANK(
CALCULATE(
MAX('Date'[Date]),
FILTER(
ALL('Date'),
'Date'[Date] > CurrentDate
&& 'Date'[Date] <= 'PromoPeriods'[MaxDate]
)
)
))
)
))
)
)
-- Return the end date if it exists, otherwise return blank
RETURN
IF(
ISBLANK(NonPromoEndDates),
BLANK(),
NonPromoEndDates
)
Visual Setup for Non-Promo Periods:
- Create a Table visual.
- Add the following fields:
Payer
Product
Non-Promo Period Start Dates (measure)
Non-Promo Period End Dates (measure)
- Filter: Apply the date slicer to this visual to see the non-promo periods within the selected time frame.
Note: These measures assume that there are no overlapping promo periods for the same Payer and Product. If there are, the measures might require adjustments to correctly identify non-promo periods. Additionally, the performance of these measures depends on the size of your dataset and the selected period. Optimizations might be necessary for very large datasets.
Example Use Case:
Suppose you have a date slicer set to January 1, 2022, to February 28, 2022. The Days Without Promo measure will calculate the total days in January and February where no promo was active for each product and payer. The Non-Promo Period Start Dates and Non-Promo Period End Dates measures will show the specific date ranges (e.g., January 1-5, and January 20-25) where no promo was running for each product and payer. This helps in identifying gaps in promotional activity.