Forum Discussion
Anonymous
6 years agoNot applicable
Convert SQL Query with Variables to DAX Query
Could someone please help me in converting this working SQL Query to a DAX Query?
SELECT [ProductNumber],
CASE
WHEN @StartDate BETWEEN [StartDate] AND [EndDate] THEN @StartDate
WHEN @StartDate < [StartDate] THEN [StartDate]
END AS [StartDate],
CASE
WHEN @EndDate BETWEEN [StartDate] AND [EndDate] THEN @EndDate
WHEN @EndDate > [EndDate] THEN [EndDate]
END AS [EndDate],
[PromoCode],
[DaysAvailableInWeek]
FROM [#Promos]
WHERE [StartDate] <= @EndDate
AND [EndDate] >= @StartDate
ORDER BY 1;
DDL:
CREATE TABLE [#Promos] ([ProductNumber] INT, [StartDate] DATE, [EndDate] DATE, [PromoCode] VARCHAR(6), [DaysAvailableInWeek] VARCHAR(20) );
DML:
INSERT INTO [#Promos] ([ProductNumber], [StartDate], [EndDate], [PromoCode], [DaysAvailableInWeek]) VALUES (6512, '2020-01-11', '2020-01-13', 'ABC123', '1, 3, 5'); INSERT INTO [#Promos] ([ProductNumber], [StartDate], [EndDate], [PromoCode], [DaysAvailableInWeek]) VALUES(6514, '2020-01-12', '2020-01-14', 'ABC123', '4, 6'); INSERT INTO [#Promos] ([ProductNumber], [StartDate], [EndDate], [PromoCode], [DaysAvailableInWeek]) VALUES(2341, '2020-01-11', '2020-01-25', '321XYZ', '1, 2');
Here is what I tried so far, but it doesn't return correct results.
EVALUATE SUMMARIZECOLUMNS(
'Dim_Promos'[PromoCode],
'Dim_Product'[ProductNumber],
'Dim_Date_Start'[FullDate],
'Dim_Date_End'[FullDate],
FILTER('Dim_Date_Start', 'Dim_Date_Start'[FullDate] <= DATE(2020, 01, 15) ),
FILTER('Dim_Date_End', 'Dim_Date_End'[FullDate] >= DATE(2019, 04, 11) ),
"Days Available In Week", [Days Available In Week])Also, I would need the date formatted as YYYY-MM-DD. When I tried the FORMAT function, it is throwing syntax error.
FORMAT('Dim_Date_Start'[FullDate], "YYYY-MM-DD"),
FORMAT('Dim_Date_End'[FullDate], "YYYY-MM-DD")Thanks!
12 Replies
- HotChilliCommunity Champion
Help us out please.
Can you provide some sample data (not a picture) please?
Also, how do you assign the two variables (from the data by row or passed from slicers)?
- AnonymousNot applicable
HotChilli I've updated my post with some sample data I'm using. I'm trying to call the DAX Query from SSRS Report/Power BI where I pass the Start and End dates to filter the data I need. Let me know if you need any other information to help me.
Thanks!
- AnonymousNot applicableFirst load you table in power bi. If you are using directquery just connect it to database.
Then create one custom date table in power bi which is not linked with any other table.
Calender function is there to create date table.
I assume you are using between slicer for start data and end date.
Add date column from date table into slicer.
Note as you mention
You want columns dynamic so it is not possible because columns get loaded at first load only and they can not updated with slicers.
So create measure
Startdate= if(max(table[startdate]) <= min(date[date])&& max(table[Enddate])>= min(date[date]),max(table[startdate]),if(min(date[date])<=max(table[startdate]),max(table[startdate])
Similarly create one more meausre for end date.
Just you need to replace min(date[date]) with max(date[date]) and startdate with enddate column.
For where clause create one conditional flag.
Flag=If(max(table[startdate]) < max(date[date]) && max(table[enddate]) > min(date[date]),1,0)
Add this measure to visual level filter and set it to 1.
Thanks,
Pravin
If it resolves your problem mark it as a solution and give Kudos.