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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

SQL to DAX conversion

How to convert below SQL query to a DAX measure. The 'date' case structure should be dymanic to get last 4 months data.

 

select  mnth,M1_Ord,M2_Ord,M3_Ord,M4_Ord,Is_cons_flg,count(outlet_id)  outlet_bucket from
(
select mnth,outlet_id, dstr_id,dstr_site_cd,M1_Ord,M2_Ord,M3_Ord,M4_Ord,
(case when M1_ord<= M2_Ord and M2_Ord<=M3_ord and M3_ord<=M4_ord then 'Y'
else 'N' end )Is_cons_flg
from (
select mnth,outlet_id,dstr_id,dstr_site_cd,(case when mnth='201901' then ord else 0  end )M1_Ord,
(case when mnth='201902' then ord else 0 end ) M2_Ord,
(case when mnth='201903' then ord else 0 end ) M3_Ord,
(case when mnth='201904' then ord else 0 end ) M4_Ord
from (
select convert(varchar(6),cal_dt,112) mnth,outlet_id,dstr_id,dstr_site_cd,count(distinct bill_doc_num) ord
from [SDDW].[FCT_DSTR_SEC_SLS_DATA]
where cal_dt between '2019-01-01' and '2019-04-30'
and ertm_type_cd='B2B'
and ctry_id=173
group by convert(varchar(6),cal_dt,112),outlet_id,dstr_id,dstr_site_cd
)freq
) cnst
) vv 
group by mnth,M1_Ord,M2_Ord,M3_Ord,M4_Ord,Is_cons_flg
4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous 

Your SQL language is complex. Could you provide me with your data model and a screenshot of the result you want?  And this will make it easier for me to understand your require.

 

Best Regards,

Rico Zhou

 

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

mwegener
Most Valuable Professional
Most Valuable Professional

Hi @Anonymous ,

 

a 1:1 translation makes no sense in my opinion.

You can filter and transform the data with Power Query while importing data.

In the report you can then visualize the data in a matrix.

The last 4 months can be filtered using a relative date filter.

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


Anonymous
Not applicable

Its a live connection. Can't do any changes in Power query. Need a DAX measure for same.

mwegener
Most Valuable Professional
Most Valuable Professional

Hi @Anonymous ,

 

can you share a screenshot of the table from the Power BI model view?

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.