Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not 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 12

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors