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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
PBI-Bro
Advocate II
Advocate II

Translating a SQL Query with ROW_NUMBER() - well beyond my current skill-level

I managed to translate a bunch of queries from SQL to DAX, but I just hit a wall with this one. My DAX knowledge is still very limited.

 

Basically, I am trying to obtain one number (measure). The number of donors whose first donation was made via digital payment methods. My SQL looks like this:

 

SELECT COUNT(t.donor_id)
FROM (
   SELECT * FROM (
         SELECT
             ROW_NUMBER() OVER(PARTITION by donor_id  ORDER BY date) AS 'RowNumber',
             donor_id,
             date,
             value ,
             c.campaign_id
          FROM campaigns c
 ) AS s
 where s.RowNumber = 1 and s.campaign_id  like '%online%'
) AS t

 

My power BI model has the tables donations (fact table) and campaigns (dim table) among many others,  

I would appreciate your help on this one!

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

hi  @PBI-Bro 

You could try this way as below:

1. create a rank measure as RowNumber

RowNumber = RANKX(FILTER(ALLSELECTED(campaigns),campaigns[donor_id]=MAX(campaigns[donor_id])),CALCULATE(MAX(campaigns[date])),,ASC)

2. create result measure

Measure 2 = COUNTAX(FILTER(campaigns,[RowNumber]=1&&SEARCH("online",[campaign_id],1,0)>0),[donor_id])

 

 

If not your case, please share your sample data and your expected output.

 

Regards,

Lin

Community Support Team _ Lin
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

3 REPLIES 3
Anonymous
Not applicable

Hey, I have this SQL QUERY Can you help me to put in dax? 

 

Select Count ( Distinct ( ID)

FROM

(Select ID ,ROW_NUMBER () over (PARTITION BY [ID] ORDER BY ID_color, id_num, id ) R

FROM Table

) q

WHERE R= 1

 

 

v-lili6-msft
Community Support
Community Support

hi  @PBI-Bro 

You could try this way as below:

1. create a rank measure as RowNumber

RowNumber = RANKX(FILTER(ALLSELECTED(campaigns),campaigns[donor_id]=MAX(campaigns[donor_id])),CALCULATE(MAX(campaigns[date])),,ASC)

2. create result measure

Measure 2 = COUNTAX(FILTER(campaigns,[RowNumber]=1&&SEARCH("online",[campaign_id],1,0)>0),[donor_id])

 

 

If not your case, please share your sample data and your expected output.

 

Regards,

Lin

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

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.