Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I have a table of dates and another table of federal holidays and what the name of that is and I just need a way to find which holiday is the closest to a given day.
EX.
Table 1:
Date | Closest Holiday |
7/6/2022 | Independence Day |
11/21/2022 | Thanksgiving |
So essentially I just need a way to populate the "Closest Holiday" column. Thanks in advance!!
Solved! Go to Solution.
@NJ81858 Add this calc column to your date table:
Closest Holiday =
VAR _currentDate = 'Date'[Date]
VAR _tbl =
ADDCOLUMNS(
Table1,
"@Diff", ABS(_currentDate - Table1[Date])
)
VAR _closeset = MINX(_tbl, [@Diff])
VAR _filtered_rows =
FILTER(
_tbl,
[@Diff] = _closeset
)
VAR _ties = TOPN(1, _filtered_rows, Table1[Date], ASC)
VAR _closest_date = CONCATENATEX(_ties, Table1[Date], " ,")
RETURN
_closest_date
@NJ81858 Add this calc column to your date table:
Closest Holiday =
VAR _currentDate = 'Date'[Date]
VAR _tbl =
ADDCOLUMNS(
Table1,
"@Diff", ABS(_currentDate - Table1[Date])
)
VAR _closeset = MINX(_tbl, [@Diff])
VAR _filtered_rows =
FILTER(
_tbl,
[@Diff] = _closeset
)
VAR _ties = TOPN(1, _filtered_rows, Table1[Date], ASC)
VAR _closest_date = CONCATENATEX(_ties, Table1[Date], " ,")
RETURN
_closest_date
You can try
Closest Holiday =
VAR currentDate =
SELECTEDVALUE ( 'Table'[Date] )
VAR closestHoliday =
SELECTCOLUMNS (
TOPN (
1,
'Holidays',
ABS ( DATEDIFF ( currentDate, 'Holidays'[Date], DAY ) ), ASC,
'Holidays'[Date], ASC
),
"@val", 'Holidays'[Name]
)
RETURN
closestHoliday
This will pick the closest holiday in the past in the event of a tie. You could change it to 'Holidays'[Date], DESC if you wanted the closest holiday in the future instead.
That filled in a holiday for me, unfortunately it filled in the same holiday for each individual date value no matter what.
User | Count |
---|---|
25 | |
12 | |
8 | |
8 | |
7 |
User | Count |
---|---|
27 | |
13 | |
13 | |
11 | |
6 |