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

SQL code to Dax expression

Hi All,

 

I need a requirement of implement below sql query in DAX. 

  declare @startdate as date
  declare @enddate as date
  set @startdate = '2021-10-18'
   set @enddate = '2021-11-15'

  select distinct count (Item_Number) from [dbo].[TblItem] as i
inner join [dbo].[Customs] as c  on i.Item_Number = c.Item_Number
  Where    ActiveDate <= @startdate and (EndDate > @enddate or EndDate is null)

However, the ActiveDate and EndDate are in 7 days interval. For example '2021-10-18' on Monday and the next date is '2021-10-25' the next Monday and soon.(7 days intervals). The startdate act as a minimum date and the EndDate is the maximum date from their respective columns and falls within 3 months 

Thanks in advance
 

 

 

 

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

I could not successfullt execute the SQL code on my side since it has error:

Ambiguous column name 'Item_Number'.

 

In Power BI, you may try:

Measure = 
var _start=DATE(2021,10,18)
var _end=DATE(2021,11,15)
return
CALCULATE(DISTINCTCOUNT(TblItem[Item_Number]), FILTER('Customs',[ActiveDate ]<=_start  && [EndDate]>= _end || [EndDate]=BLANK()))

Eyelyn9_0-1642475345836.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

I could not successfullt execute the SQL code on my side since it has error:

Ambiguous column name 'Item_Number'.

 

In Power BI, you may try:

Measure = 
var _start=DATE(2021,10,18)
var _end=DATE(2021,11,15)
return
CALCULATE(DISTINCTCOUNT(TblItem[Item_Number]), FILTER('Customs',[ActiveDate ]<=_start  && [EndDate]>= _end || [EndDate]=BLANK()))

Eyelyn9_0-1642475345836.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

smpa01
Super User
Super User

@Anonymous  

declare @startdate as date 
declare @enddate as date 
set
   @startdate = '2021-10-18' 
   set
      @enddate = '2021-11-15' 
      select distinct
         count (Item_Number) 
      from
         [dbo].[TblItem] as i 
         inner join
            [dbo].[Customs] as c 
            on i.Item_Number = c.Item_Number 
      Where
         ActiveDate <= @startdate 
         and 
         (
            EndDate > @enddate 
            or EndDate is null
         )

this SQL looks ambiguous to me because it is not clear whether the filtering applies in 

[dbo].[TblItem]

or

[dbo].[Customs]

 

You can achieve a basic inner join like this

basicMeasure = calculate(distinctcount(item[Item_Number]),Customs)

//if filtering item
m1 = calculate(DISTINCTCOUNT(Item[Item_Number]),Filter(Item, Item[ActiveDate]<=DAY(2021,10,18)||(Item[EndDate]>=DAY(2021,11,15)||Item[EndDate]=BLANK()),(Customs))

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
smpa01
Super User
Super User

@Anonymous  are 

[Customs]

and

[TblItem]

related?

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

@smpa01 Yes. they are related or joined with item_Number from both tables.

The ActiveDate and EndDate are both from TblItem

 

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.