How get a list of date from an expression in POWER BI DAX - list

I am trying to find a way how to get the list of date when i do this expression in POWER BI with DAX :
For exemple : 16/11/2025 - 3
The output should be like this ( the last 3 Dates):
16/11/2024
16/11/2023
16/11/2022
EDITS:
I need just some edit about the Output if it possible.
My Inputs:
column1 that contains dates.
Column2 that contains the numbers
1- for exemple when i do this ( column1-Column2) :
10/04/2024 - 1 (Output)=> 10/04/2023
10/04/2023 - 0 (Output)=> 10/04/2023 ( stay the same)
10/04/2026 - 3 (Output)=> 10/04/2025 - 10/04/2024 - 10/04/2023
-And is it possible to store the dates in a list or table because i'm gonna use it later in other formula.
Thanks.

Please Crete a new table (see picture below) and paste this code:
DateTable =
VAR FixedDate =
DATE ( 2025, 11, 16 )
VAR PrevDate = FixedDate - 365
VAR TwoYearBack = PrevDate - 366
VAR ThreeYearBack = TwoYearBack - 365
RETURN
{ PrevDate, TwoYearBack, ThreeYearBack }
VERSION_2
A Better solution from #RonRosenfeld:
DateTable =
VAR FixedDate =
DATE ( 2025, 11, 16 )
VAR PrevDate = DATE(YEAR(FixedDate)-1,MONTH(FixedDate), DAY(FixedDate))
VAR TwoYearBack = DATE(YEAR(PrevDate)-1,MONTH(PrevDate), DAY(PrevDate))
VAR ThreeYearBack = DATE(YEAR(TwoYearBack)-1,MONTH(TwoYearBack), DAY(TwoYearBack))
RETURN
{PrevDate, TwoYearBack, ThreeYearBack}
And Result:

Related

Issue related to PowerBI DAX measure

I am facing 1 issue related to DAX, let me show you the scenario.
DAX for ActualUpdates_60min_Te*
ActualUpdates_60min_Te* =
VAR tbl60 =
CALCULATETABLE(
VALUES(EventDetail[ShipmentKey])
,ALL(EventDetail[TimelinessBreak])
,FILTER(
EventDetail
,EventDetail[TimelinessBreak] = "<= 1 Hr"
)
) // Returns 893 records
VAR tbl30 =
CALCULATETABLE(
VALUES(EventDetail[ShipmentKey])
,ALL(EventDetail[TimelinessBreak])
,FILTER(
EventDetail
,EventDetail[TimelinessBreak] = "<= 30 Mins"
)
) // Returns 301 records
VAR CntRows = COUNTROWS(EXCEPT(tbl60,tbl30)) // Returns 21 records
VAR C = CntRows + [ActualUpdates_30min*] // Returns 914 records
Return C
If you see the Card - It is returning 914, when it is showing in a table it is showing 301.
What I need to achieve is I should have 914 in a table timelinessbreak is <= 1Hr.
Can anyone please help in achieving this.

Calculating single values from cumulative data in Power BI

In my sales table I would like to change cumulative values into single values. Here is sample data from my table.
I created a measure that as far as I know should works for this.
sales_single_values = VAR current_sales = SUM('sales'[sales cumulative]) VAR prev_sales SUM('sales'[sales cumulative]) - CALCULATE( current_sales, 'sales'[period] = 'sales'[period] - 1) Return IF(ISBLANK(prev_sales), BLANK(), current_sales - prev_sales)
But unfortunately the final result on the chart is still the same as I used cumulative values, not single ones. Any ideas what should I change in my measure?
Expected values would be:
Period 1: 4
Period 2: 2
Period 3: 7
As a measure, something like:
sales_single_values =
var prev = max('sales'[period]) - 1
var prev_sales = CALCULATE( SUM('sales'[sales cumulative]), 'sales'[period] = prev)
Return
sum(sales[sales cumulative]) - prev_sales
But this seems like more of a modeling transformation, so do it in PQ, or with a calculated column, like
current sales =
var prev = calculate( max('sales'[period]) ) - 1
var prevSales = calculate( sum(sales[sales cumulative]), all(sales), sales[period] = prev)
return
sales[sales cumulative]-prevSales

DAX IF Statement Can't Use Table Column

I have the following DAX expression:
Students_Who_Viewed = var max_report_date = TOPN(1,DISTINCT(table_a_Historical[ReportDate]),table_a_Historical[ReportDate])
var last_week_report_date = max_report_date - 2
/////
var this_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = max_report_date)
)
/////
var last_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = last_week_report_date)
)
/////
var students_viewed_wow_change = COALESCE(this_week_student_viewed,0) - COALESCE(last_week_student_viewed,0)
/////
RETURN
IF (
**table_a_Historical[exempt_status_cy] = "exempt" || table_a_Historical[exempt_status_py],**
"N/A",
IF (
students_viewed_wow_change > 0 //This means views this week is more than last week.
,1
,IF (
students_viewed_wow_change < 0 //This means there were less views this week than last week
,-1
,0 //This value will be used if there is no change in views this week relative to last week.
)
)
)
In the part surrounded by **, I keep getting an error message. Those columns are valid in that table. Why is this?
As DAX search for aggregation, can you please try this below where you directly referred to the column name:
min(table_a_Historical[exempt_status_cy])
Hopefully this will work.

Sum row with next row value and grab next value in other column for the date selected

Let's say I have this data:
Earn Earn Cum.
13-Apr - -
14-Apr 48 48
15-Apr 257 305
16-Apr 518 823
17-Apr 489 1,312
18-Apr 837 2,149
19-Apr 1,005 3,154
20-Apr 1,021 4,175
21-Apr 1,463 5,638
22-Apr 2,630 8,268
23-Apr 2,993 11,261
24-Apr 3,354 14,615
25-Apr 4,332 18,947
26-Apr 4,885 23,832
27-Apr 4,514 28,346
28-Apr 4,356 32,702
29-Apr 4,824 37,526
30-Apr 7,082 44,608
1-May 6,091 50,699
2-May 1,407 52,106
When a date is selected in a dropdown slicer for example: 1-May I'd like to sum the rows 1-May with the next row 2-May for the column Earn and grab the next value 52,106 for the column Earn Cum.. The result should be:
1-May 7,498 52,106
Another example: if the date selected was 30-Apr the result must be:
30-Apr 13,173 50,699
I'm scratching my head trying to do this using a measure in Power BI.
I'll call your table "Data".
Create a measure:
Next Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE (
SUM ( Data[Earn] ),
Data[Date] = Current_Date || Data[Date] = Next_Date
)
Create another measure:
Next Cum Earn =
VAR Current_Date = MAX ( Data[Date] )
VAR Next_Date = Current_Date + 1
RETURN
CALCULATE ( SUM ( Data[Earn Cum] ), Data[Date] = Next_Date )
Result:
Note: the code assumes that your dates are sequential (no gaps). If they have gaps, things are a bit more complex.
It will help to have access to date intelligence, so create a date table if you don't have one already. The following dax sets up a small table that just barely covers the sample data in your example.
Date = CALENDAR(Date(2019,4,10),Date(2019,5,5))
Create a relationship between the Dates in your table and the new Date dimension. Add a slicer to your visuals using the Date dimension.
We can use IF and ISFILTERED to check if filtering is being done. If we aren't filtering on Date then we get the normal table behavior. If we are, we'll see our modified result.
Earn _ Alt =
var tomorrow = CALCULATE (
SUM(Table1[Earn]),
dateadd('Table1'[Date], 1, DAY)
)
return Sum(Table1[Earn]) + IF(ISFILTERED`('Date'[Date]),tomorrow,0)`
and
Earn Cum. _ Alt = IF(ISFILTERED('Date'[Date]),
CALCULATE (
SUM(Table1[Earn Cum.]),
dateadd('Table1'[Date], 1, DAY)
),
SUM(Table1[Earn Cum.]))
Results:
-Unfiltered-
-Filtered-

Calculate latest date for every ID with DAX

I have a dataset in Power BI like this:
ID FirstDate LastDate
214443 07/06/2016 07/06/2017
214443 09/11/2016 09/11/2017
214443 28/09/2018 11/06/2019
214443 31/05/2019 11/06/2019
I would like to create two calculated columns that contain the latest date for every index. Currently, I am only able to get the latest date of all the dataset or the same date for every row.
The output should be this:
ID FirstDate LastDate FirstDate2 LastDate2
214443 07/06/2016 07/06/2017
214443 09/11/2016 09/11/2017 07/06/2016 07/06/2017
214443 28/09/2018 11/06/2019 09/11/2016 09/11/2017
214443 31/05/2019 11/06/2019 28/09/2018 11/06/2019
Thanks!!
I started with a data like this,
And then added the Index Column using the query editor,
And then created a column to check the number of times a single ID is present :-
Number of ID's Present = COUNTROWS(FILTER('MyTable',
(EARLIER('MyTable'[ID]) = 'MyTable'[ID])))
And then wanted to know the starting Index of each ID's to make it easy :-
Starting Index = CALCULATE(MIN(MyTable[Index]),FILTER('MyTable',
(EARLIER('MyTable'[ID]) = 'MyTable'[ID])))
And then now you can do what you are looking for with some easy ways,
First Date 2 =
Var NumberofIDsPresent = MyTable[Number of ID's Present]
Var StartingIndex = MyTable[Starting Index]
Var CurrentIndex = MyTable[Index]
Var Only_OneTime_ID_Present = IF(OR(NumberofIDsPresent = 1,CurrentIndex = StartingIndex),1,0)
Var Multiple_Times_ID_Present = CALCULATE(MIN(MyTable[First Date]),FILTER(MyTable, MyTable[Index] = CurrentIndex - 1))
var result = IF(Only_OneTime_ID_Present = 1, BLANK(),Multiple_Times_ID_Present)
return result
Last Date 2 =
Var NumberofIDsPresent = MyTable[Number of ID's Present]
Var StartingIndex = MyTable[Starting Index]
Var CurrentIndex = MyTable[Index]
Var Only_OneTime_ID_Present = IF(OR(NumberofIDsPresent = 1,CurrentIndex = StartingIndex),1,0)
Var Multiple_Times_ID_Present = CALCULATE(MIN(MyTable[Last Date]),FILTER(MyTable, MyTable[Index] = CurrentIndex - 1))
var result = IF(Only_OneTime_ID_Present = 1, BLANK(),Multiple_Times_ID_Present)
return result
Now, this makes my data look something like this,
Please accept the answer if it helps and Kindly let me know, if it doesn't solve your problem.
Take the max over the FirstDate column only considering rows with matching ID and dates before the current row's date.
Previous Date =
CALCULATE (
MAX ( Table1[FirstDate] ),
FILTER (
ALLEXCEPT ( Table1, Table1[ID] ),
Table1[FirstDate] < EARLIER ( Table1[FirstDate] )
)
)