SQL to DAX: I am hoping someone could help me rewrite this SQL code in DAX.

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
921
Office Version
  1. 365
Platform
  1. Windows
I am a beginner in both SQL and DAX. I have been re-creating a Query from Excel into Power BI but writing the calculation for this one column has me stumped and no one at my company knows the program well enough.
Here is the SQL calculation:

SQL:
ROUND((SELECT SUM(IF(time_resumed = 0, UNIX_TIMESTAMP(), time_resumed) - time_paused) / 86400 FROM erp_workorder_inactive WHERE id_workorder = t1.id_primary))  AS `Paused Days (Total)`

The farthest I have got in DAX is below, but have no idea how to do the rest:

Code:
= Table.AddColumn(#"SubTotal Calc", "Paused Days", each ([winnipeg.erp_workorder_inactive.time_resumed]-[winnipeg.erp_workorder_inactive.time_paused])/86400)

Thank you to anyone who can help!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
DAX is not SQL. The languages are approach to the tools are different. While it might be possible to get a technical match with DAX code against your SQL, it is almost guaranteed that what you will end up with is not what you really need.

how about you post your table structure and explain the problem you have.
 
Upvote 0
I will see if I can try and explain it better. I have only been working with both DAX and SQL for a few days...

so in DAX I wrote:
Code:
= Table.AddColumn(#"Removed Columns1", "Paused Days", each Number.RoundUp((([winnipeg.erp_workorder_inactive.time_resumed]-[winnipeg.erp_workorder_inactive.time_paused])/86400),0))

Which works for some of the rows I have but not all and I am not sure why.
I did not write the SQL code, someone else did and it appears it is converting seconds to days and rounding but then I get some weird results for other rows like the picture below (the result should be 43 in the original SQL code but I get -18677:

1616596255258.png
 
Upvote 0
As Matt said a sample of your data and a description of what you want would really help.
You are not using DAX, the sample of code provided is M language used in Power Query. This is totally different to DAX. The calculation you provided is mathematically correct (check it with a calculator). I think you need to group the data in some way to aggregate the time differences. I would perhaps be using M to enter and transform the data before loading into the data model, then you can use a function like SUMX in a DAX measure the find the sum of the difference.
 
Upvote 0
As Matt said a sample of your data and a description of what you want would really help.
You are not using DAX, the sample of code provided is M language used in Power Query. This is totally different to DAX. The calculation you provided is mathematically correct (check it with a calculator). I think you need to group the data in some way to aggregate the time differences. I would perhaps be using M to enter and transform the data before loading into the data model, then you can use a function like SUMX in a DAX measure the find the sum of the difference.
Ok, I think I understand what you are saying. Like I said I am brand new to power query.
My data is coming from an ERP system. The original Query was written by someone in IT who used SQL language to clean it up and put it into excel.
I wanted to replicate this into power query and have almost done so minus this paused day column.
To help I have put the data in Excel. For each workorder there is time resumed and time paused. Somehow in the original Query made by IT they used this data and calculated how much time the work was paused for in days. Not only am I unsure on the math but I do not know how to replicate this in power query. I hope I have provided what you guys need in order to help. The yellow column is the conclusion IT came to.

Workorders_test.xlsx
ABCDEF
1W/O NumberDate ReceivedDate ShippedTime PausedTime ResumedPaused Days (Total)
21127482021-02-042021-03-121612980622047
31127472021-02-032021-02-261613665602039
41125942021-01-212021-03-101612874926048
51125132021-01-152021-02-11161125647516117742026
61124752021-01-132021-02-04161116281716112335451
71124182021-01-062021-01-281610630042074
81124172021-01-062021-01-201610456383076
91123982021-01-042021-01-22160993898816101099502
101121592020-12-1101611930477059
111121072020-12-072021-01-141607440405161045899335
121117862020-11-1201613998777035
131117852020-11-122021-03-261612371033054
141116802020-11-02016044157070146
151116792020-11-02016044156920146
161116642020-10-282021-02-0416044240760146
171115502020-10-222020-12-0916037195010154
181115492020-10-222020-12-09160371947816037342740
191115482020-10-222020-12-1116037194470154
201115472020-10-222020-12-0916037194200154
211115302020-10-212020-11-2616037187210154
221114462020-10-152021-02-24160328499416034770152
231112892020-09-242021-03-1216050113050139
241108572020-08-142020-10-2616007876260188
251108562020-08-142020-10-2616007876130188
261108542020-08-142020-10-2616007876400188
271107902020-08-062021-02-1916026028120167
281105722020-07-132020-08-0715955989640248
291105212020-07-07016000819201601315429196
301105002020-07-062020-12-0315954262091597858209226
311104782020-06-302020-10-2916039062650152
321104362020-06-292021-01-12160017792716003459402
331103572020-06-222020-08-2815929357790279
341103562020-06-222020-08-2815929381300279
351103292020-06-172020-10-2315931738251594060735260
361103142020-06-162021-01-2815923362490286
371101842020-06-022021-03-1115922353940287
381099132020-05-052021-02-1115924943660284
391098532020-04-272020-10-221598882182160321143550
401098462020-04-27015886990170328
411098222020-04-212020-05-1415879941660336
421094682020-03-12015841156721584368360374
431094532020-03-122020-09-1815841156311584368323377
441091772020-02-202021-02-2615843812580378
451091762020-02-202020-06-0515843812960378
461083772019-12-202020-04-2915768758310465
471065592019-07-222020-04-1515864375200354
481061532019-06-1301610651328074
491057212019-04-292020-02-21157668311915766832490
Sheet4
 
Upvote 0
Not sure how they are accounting for the ones with zeros for the 'Time Resumed', but here's the basic formula for finding out the number of days.

Book2
ABCDEFG
1W/O NumberDate ReceivedDate ShippedTime PausedTime ResumedPaused Days (Total)Days Formula
21127482/4/20213/12/20211612980622047 
31127472/3/20212/26/20211613665602039 
41125941/21/20213/10/20211612874926048 
51125131/15/20212/11/20211611256475161177420266
61124751/13/20212/4/20211611162817161123354511
71124181/6/20211/28/20211610630042074 
81124171/6/20211/20/20211610456383076 
91123981/4/20211/22/20211609938988161010995022
1011215912/11/202001611930477059 
1111210712/7/20201/14/2021160744040516104589933535
1211178611/12/202001613998777035 
1311178511/12/20203/26/20211612371033054 
1411168011/2/2020016044157070146 
1511167911/2/2020016044156920146 
1611166410/28/20202/4/202116044240760146 
1711155010/22/202012/9/202016037195010154 
1811154910/22/202012/9/20201603719478160373427400
1911154810/22/202012/11/202016037194470154 
2011154710/22/202012/9/202016037194200154 
2111153010/21/202011/26/202016037187210154 
2211144610/15/20202/24/20211603284994160347701522
231112899/24/20203/12/202116050113050139 
241108578/14/202010/26/202016007876260188 
251108568/14/202010/26/202016007876130188 
261108548/14/202010/26/202016007876400188 
271107908/6/20202/19/202116026028120167 
281105727/13/20208/7/202015955989640248 
291105217/7/202001600081920160131542919614
301105007/6/202012/3/20201595426209159785820922628
311104786/30/202010/29/202016039062650152 
321104366/29/20201/12/20211600177927160034594022
331103576/22/20208/28/202015929357790279 
341103566/22/20208/28/202015929381300279 
351103296/17/202010/23/20201593173825159406073526010
361103146/16/20201/28/202115923362490286 
371101846/2/20203/11/202115922353940287 
381099135/5/20202/11/202115924943660284 
391098534/27/202010/22/2020159888218216032114355050
401098464/27/2020015886990170328 
411098224/21/20205/14/202015879941660336 
421094683/12/20200158411567215843683603743
431094533/12/20209/18/2020158411563115843683233773
441091772/20/20202/26/202115843812580378 
451091762/20/20206/5/202015843812960378 
4610837712/20/20194/29/202015768758310465 
471065597/22/20194/15/202015864375200354 
481061536/13/201901610651328074 
491057214/29/20192/21/20201576683119157668324900
Sheet3
Cell Formulas
RangeFormula
G2:G49G2=IF(E2=0,"",ROUND((E2-D2)/(24*60*60),0))
 
Upvote 0
Book3
ABCDEFGHIJ
1W/O NumberDate ReceivedDate ShippedTime PausedTime ResumedPaused Days (Total)report date3/30/2021
21127482/4/20213/12/2021161298062204747base date1/1/1970
31127472/3/20212/26/2021161366560203939
41125941/21/20213/10/2021161287492604848
51125131/15/20212/11/20211611256475161177420266
61124751/13/20212/4/20211611162817161123354511
71124181/6/20211/28/2021161063004207474
81124171/6/20211/20/2021161045638307676
91123981/4/20211/22/20211609938988161010995022
1011215912/11/20200161193047705959
1111210712/7/20201/14/2021160744040516104589933535
1211178611/12/20200161399877703535
1311178511/12/20203/26/2021161237103305454
1411168011/2/2020016044157070146146
1511167911/2/2020016044156920146146
1611166410/28/20202/4/202116044240760146146
1711155010/22/202012/9/202016037195010154154
1811154910/22/202012/9/20201603719478160373427400
1911154810/22/202012/11/202016037194470154154
Sheet1
Cell Formulas
RangeFormula
G2:G19G2=ROUND(IF(E2=0,(($J$1-$J$2)*3600*24 - D2)/3600/24,(E2-D2)/3600/24),0)
 
Upvote 0
Book3
ABCDEFGHIJ
1W/O NumberDate ReceivedDate ShippedTime PausedTime ResumedPaused Days (Total)report date3/30/2021
21127482/4/20213/12/2021161298062204747base date1/1/1970
31127472/3/20212/26/2021161366560203939
41125941/21/20213/10/2021161287492604848
51125131/15/20212/11/20211611256475161177420266
61124751/13/20212/4/20211611162817161123354511
71124181/6/20211/28/2021161063004207474
81124171/6/20211/20/2021161045638307676
91123981/4/20211/22/20211609938988161010995022
1011215912/11/20200161193047705959
1111210712/7/20201/14/2021160744040516104589933535
1211178611/12/20200161399877703535
1311178511/12/20203/26/2021161237103305454
1411168011/2/2020016044157070146146
1511167911/2/2020016044156920146146
1611166410/28/20202/4/202116044240760146146
1711155010/22/202012/9/202016037195010154154
1811154910/22/202012/9/20201603719478160373427400
1911154810/22/202012/11/202016037194470154154
Sheet1
Cell Formulas
RangeFormula
G2:G19G2=ROUND(IF(E2=0,(($J$1-$J$2)*3600*24 - D2)/3600/24,(E2-D2)/3600/24),0)
JGordon11 This is perfect! but how do I write that in a calculated column in the data module in Power BI?

I tried writing it this way but get an error:

Code:
Column = ROUND(IF('winnipeg erp_workorder'[Time Resumed]=0,((Today()-01-01-1979)*3600*24-'winnipeg erp_workorder'[Time Paused]/3600/24+1,('winnipeg erp_workorder'[Time Resumed]-'winnipeg erp_workorder'[Time Paused])/3600/24),0))

I am brand new to Power BI, thank you for your patience!
 
Upvote 0
You need to access the report date (the date the report was run and the data table created). Once you have that you would create a conditional column essentially following the formula. Leave out the round part and do that to the new column in a second step. Without knowing where to have PQ access the report date the conditional column is not going to be able handle the 0's in the Time Resumed column.
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,094
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top