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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
abraatz
New Member

Convert a DAX query for use in Query Editor

I would like to be able to recreate the steps below for use in Query Editor using M, rather than DAX, so it can be recreated and run against numerous queries, before appending them together.

 

The initial query returns several columns but only two are needed to create the necessary calculated columns (Date in descending order, Adj Close). I also create an added column in Query Editor to assign a constant Name to all rows. I use the following steps to get to the final value that I would like by creating additional columns.

 

Column 1 - PrevDate - there are gaps in dates so need to use a Max function to find max date less than current row date

PrevDate = CALCULATE(MAX(Query1[Date]), (FILTER(Query1,EARLIER(Query1[Date])>Query1[Date])))

Column 2 - PreAdjClose

 

PreAdjClose = CALCULATE(SUM(Query1[Adj Close]), (FILTER(Query1, EARLIER(Query1[Ticker])=Query1[Ticker] && EARLIER(Query1[PrevDate])=Query1[Date])))

Column 3 - DayReturn - need to apply blank to final row, since it will return infinity otherwise

 

DayReturn = IF(ISBLANK([PrevDate]), Blank(), Query1[Adj Close] / Query1[PreAdjClose] - 1)

If it is possible to skip the PrevDate column and just retrieve the value from the next row Adj Close (since descending order), I do not necessarily need that column.

 

 

Any help would be greatly appreciated.

7 REPLIES 7
dkay84_PowerBI
Microsoft Employee
Microsoft Employee

Also, please note that if you are going to calculate the cumulative return over a given period, you can leave the -1 off of the return equation and just do a multiplication of all the daily returns (I think PRODUCTX is the function to use here.  See: https://msdn.microsoft.com/en-us/library/dn802544.aspx)

 

 

dkay84_PowerBI
Microsoft Employee
Microsoft Employee

From reading your measure names, I assume you are tracking the adj. close price of a stock and trying to calculate the daily return based on the previous day's adj. close vs. the current day's.  In other words, you want adjusted close in row 2 divided by adjusted close in row 1.

 

I think the easiest way to do this is to create a copy of your table, then add an index to Table 1 starting from 0, and an index for Table 2 starting at 1.  Then merge the two tables by index and you will get a row offset for Adjusted Close.  Then a simple calc column can calculate the daily return.

ImkeF
Community Champion
Community Champion

Hi, this would be much easier for me if you could also paste some samples or screenshots of your data please.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Below is a screen clip of sample data:
 data.jpg

You will get more mileage if you paste that as a table.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Date  Open High Low Close Volume AdjClose Ticker PrevDate PreAdjClose DayReturn 

Friday, April 28, 2017160.5160.589996159.699997160.2899934154100158.739298IBMThursday, April 27, 2017158.769022-0.000187215362452808
Thursday, April 27, 2017160.289993160.479996159.630005160.3200074122600158.769022IBMWednesday, April 26, 2017158.5115270.00162445599303318
Wednesday, April 26, 2017160.529999161.100006159.889999160.0599984327800158.511527IBMTuesday, April 25, 2017158.838337-0.00205750076569988
Tuesday, April 25, 2017161.779999162.039993160.380005160.3899994860400158.838337IBMMonday, April 24, 2017159.194855-0.00223950704939546
Monday, April 24, 2017161.289993161.570007160.419998160.755320600159.194855IBMFriday, April 21, 2017158.8284390.00230699238944232
Friday, April 21, 2017162.050003162.110001160.380005160.3800055607800158.828439IBMThursday, April 20, 2017160.729862-0.0118299298981541
Greg_Deckler
Community Champion
Community Champion

Those do not look to be particularly easy to recreate in M. If anyone can help it is @ImkeF



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

Top Kudoed Authors