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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

How do I translate this sql query into DAX and get the visual I want to have?

Hi,

 

I want to make such a visual. % of customers in different Product.  I have product hierarchy and customer IDs. 

lyao716_0-1639573510038.png

lyao716_1-1639573567534.png

However, Customer IDs is from our order table. Which means it is not necessary the lastest purchase that the customer made. I want to have the lastest product they purchased and see how that distributed in different groups.
I have a sql query like this:

select max(PaymentDateTime),Key_Profiles, Key_ProductDetails
from [edw].[FactOrders]
group by Key_Profiles,Key_ProductDetails
order by key_profiles

 

How can I create a measure that filter only the lastest purchase? 


Thank you very much for helping.

 

BR,

Lin

 

 

5 REPLIES 5
smpa01
Super User
Super User

@Anonymous  the equivalent measure is

Measure = calculate(max(tbl[PaymentDatetIme]),allexcept(tbl,tbl[Key_Profiles],tbl[Key_ProductDetails]))

 

once you have that, put Key_Profiles, Key_ProductDetails on X axis and use the measure on Y axis

 

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

Thank you very much. I will try it later. I actually don't need Key_ProductDetails from order table. This is the foreign key that is connected with primary key in the Table called 'product'.  And that's where Axis field come from. I am using product name hierarchy from 'Product' table. So in this way,  do I need to change your formula? like

Measure = calculate(max(tbl[PaymentDatetIme]),allexcept(tbl,tbl[Key_Profiles],'product'[Key_ProductDetails]))

?

@Anonymous  I am having trouble visualizing the req. Is it kindly possible to cretae a pbix in a samll scale and post here or provide a SQL DDL.

 

I was simply translating the query.

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

This is what I wrote:

Customer_Latest_Purchase =
 
CALCULATE(
count('edw FactOrders'[Key_Profiles]),
Filter('edw FactOrders','edw FactOrders'[PaymentDateTime] = max('edw FactOrders'[PaymentDateTime]))
)
But the number is not correct
lyao716_0-1639578017852.pnglyao716_1-1639578100501.png

 

amitchandak
Super User
Super User

@Anonymous , Max on the column  PaymentDateTime in a visual should do

 

measure examples

 

max(FactOrders[PaymentDateTime])

 

or

maxx(summarize(FactOrders, [Key_Profiles], [Key_ProductDetails], "_1", max(FactOrders[PaymentDateTime])  ) , [_1] )

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.