Power Query Get Duration Null Date

svacharya

New Member
Joined
Aug 10, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a data set in CSV format that has multiple date fields. I have loaded the data into Power Query and set the Data Type to date. I am attempting to calculate the Duration (in days) between two dates. I get bunch of errors because some of the values in the Date columns are null. Is there a better way to calculate Duration between two dates? Below is the PQ code that i used.

Power Query:
Table.AddColumn(#"Changed Type1", "xDuration", each Duration.Days([[FINAL_INSTRELEASEDATE]] - [HNSUPLOADDT]))
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I can think of two options since you did not tell us what you want to happen when there is a null value. Should it be ignored? then look at my #2. If you wish to have it be zero then look at my #1.

1. For the nulls, do a replace and replace them with the date value you are comparing to so that you end up with a zero value.
2. Filter out the rows with null values.

Without more information on what you want and actual examples shown, this is the best I can offer.
 
Upvote 0
Thank you for taking the time to respond. Below is a description of the data and what i am attempting to do (highlevel).
  1. My dataset tracks progress of products through various "gates" for customers.
  2. There are multiple customers and a finite number of products (say 10).
  3. Each product has four possible gates to traverse (as an example receive , build , Quality Control and ship) and therefore 4 completion dates (represented in columns).
  4. I have attached an image of a sample data set
  5. The end result is to graphically represent elapsed time between tasks for a given product/Customer. I have attached the output as well.
  6. i want to determine the duration between Task1 and Task2. Task2 and Task3. Task3 and Task4.
  7. I cannot filter out the Nulls because the record is valid.
  8. If a date is null, i would like the duration calculation to be either blank or null (not zero)
I have built this successfully in excel (non PQ) using formulas etc. To mitigate null (non PQ), i check for "null"condition and calculate the duration. But i prefer to load and manipulate the data in power query due to the sheer number of records.

i have not been successful in checking for null and setting the value accordingly in PQ. I have NOT tried error handling (equivalent of IFERROR) as possible solution. I am novice with M Code and still trying to figure this out.

Appreciate any guidance! Thanks in advance!
 

Attachments

  • image_2021-08-11_185734.png
    image_2021-08-11_185734.png
    11.7 KB · Views: 13
  • image_2021-08-11_185748.png
    image_2021-08-11_185748.png
    6.7 KB · Views: 13
Upvote 0
Cannot manipulate Data in a picture. Please use the XL2BB function to provide data. Help us to help you by giving us something we can work with and not require us to recreate your data to fix your problem.
 
Upvote 0
Attached

Book1
ABCDEFG
1CustomerProductTask1Task2Task3Task4
2Customer1Product19/1/20 4:009/3/20 12:3010/13/20 12:47
3Customer1Product211/12/20 5:0011/14/20 9:5311/16/20 9:5512/2/20 11:11
4Customer1Product412/3/20 5:0012/8/20 13:1912/22/20 11:59
5Customer2Product41/4/21 0:001/4/21 13:012/15/21 18:182/24/21 12:31
6Customer3Product31/4/21 0:001/4/21 13:01
7Customer4Product28/3/20 4:008/4/20 12:15
8Customer5Product18/3/20 4:008/6/20 12:15
9Customer6Product38/3/20 4:008/8/20 12:158/22/20 12:208/28/20 15:56
10
11
12
13CustomerElapsed Time1Elapsed Time2Elapsed Time 3
14Customer1240
15Customer12216
16Customer1514
17Customer21429
18Customer31
19Customer41
20Customer53
21Customer65146
22
23
Sheet1
Cell Formulas
RangeFormula
D15,B21:D21,B14:B20,C17:D17,C14:C16D15=F3-E3
 
Upvote 0
I have achieved your end result using Power Query. Here are two different ways to get it

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"Product", type text}, {"Task1", type number}, {"Task2", type number}, {"Task3", type number}, {"Task4", type number}}),
    #"Inserted Subtraction" = Table.AddColumn(#"Changed Type", "Subtraction", each [Task2] - [Task1], type number),
    #"Renamed Columns" = Table.RenameColumns(#"Inserted Subtraction",{{"Subtraction", "Elapsed Time1"}}),
    #"Inserted Subtraction1" = Table.AddColumn(#"Renamed Columns", "Subtraction", each [Task3] - [Task2], type number),
    #"Renamed Columns1" = Table.RenameColumns(#"Inserted Subtraction1",{{"Subtraction", "Elapsed Time2"}}),
    #"Inserted Subtraction2" = Table.AddColumn(#"Renamed Columns1", "Subtraction", each [Task4] - [Task3], type number),
    #"Renamed Columns2" = Table.RenameColumns(#"Inserted Subtraction2",{{"Subtraction", "Elapsed Time3"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns2",{"Product", "Task1", "Task2", "Task3", "Task4"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Elapsed Time1", Int64.Type}, {"Elapsed Time2", Int64.Type}, {"Elapsed Time3", Int64.Type}})
in
    #"Changed Type1"

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Inserted Date Subtraction" = Table.AddColumn(Source, "Elapsed Time1", each Duration.Days([Task2] - [Task1]), Int64.Type),
    #"Inserted Date Subtraction1" = Table.AddColumn(#"Inserted Date Subtraction", "Elapsed Time2", each Duration.Days([Task3] - [Task2]), Int64.Type),
    #"Inserted Date Subtraction2" = Table.AddColumn(#"Inserted Date Subtraction1", "Elapsed Time3", each Duration.Days([Task4] - [Task3]), Int64.Type),
    #"Removed Other Columns" = Table.SelectColumns(#"Inserted Date Subtraction2",{"Elapsed Time3", "Elapsed Time2", "Elapsed Time1", "Customer"})
in
    #"Removed Other Columns"
 
Upvote 0
Solution
Thanks for the feedback. Glad to hear it is working for you.
 
Upvote 0

Forum statistics

Threads
1,221,713
Messages
6,161,463
Members
451,708
Latest member
PedroMoss2268

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