Forum Discussion

James_Galis1's avatar
James_Galis1
Helper II
2 years ago
Solved

Fill Down on conditions

Hello

I have a table with with null values where i wish to populate the data based on conditions and i am not sure if possible or how to go about it

 

 

Note: The earning code ( TTBV ) is applied when there is a gap between visits attended to on the same day.

 

Condition1.

The visit start time ( null values ) should contain the end time of the previous visit when there is a gap between visits

Condition2.

The visit end time ( null values ) should be the addition of the new start time based on condition 1 , plus the value in quantity field ( not sure if converting to duration output is the correct option for the quantity column )

 

Below is the expected output when manually entering these values.

 

 

Appreciate the assistance

 

 

  • spinfuzer's avatar
    spinfuzer
    2 years ago

    See file for example.

     

     

    let
        Source = Excel.CurrentWorkbook(){[Name="pay_test"]}[Content],
        changeType = Table.TransformColumnTypes(Source,{{"Emp. #", type text}, {"Employment Type", type text}, {"Type", type text}, {"Visit ID", Int64.Type}, {"Employee Cost Centre", type any}, {"Cost Centre Description", type any}, {"Visit start date", type text}, {"Visit end date", type text}, {"Visit start time", type time}, {"Visit end time", type time}, {"Service type", type text}, {"Earning Code", type text}, {"Quantity", type number}, {"Premium Quantity", type any}, {"Units", type text}, {"Override rate", Int64.Type}, {"Pay amount", Int64.Type}}),
        priorStep = Table.SelectRows(changeType, each ([Service type] <> "Reimburse KMs Travelled")),
        eligile_index =
            let 
                buffer_date = List.Buffer(priorStep[Visit start date]), 
                buffer_start = List.Buffer(priorStep[Visit start time]),
                buffer_end = List.Buffer(priorStep[Visit end time])
            in
                List.Generate(
                    () => {0}, // current, index
                    each _{0} < List.Count(buffer_start) - 1,
                    each { _{0} + 1},
                    each 
                        let x = (buffer_start{_{0}+1} ?? buffer_start{_{0}+2}) - buffer_end{_{0}}
                        in if buffer_start{_{0}} <> null 
                        then Duration.TotalMinutes(x) < 60 and Duration.TotalMinutes(x) >= 0
                        else null
                ),
        add_eligible_index = 
            Table.Buffer(
                Table.FromColumns(
                    Table.ToColumns(priorStep)&{eligile_index},
                    Table.ColumnNames(priorStep)&{"Eligible"}
                )
            ),
    /*    #"Added Index" = Table.AddIndexColumn(#"Filtered Rows", "Index", 0, 1, Int64.Type),
        #"Added Eligible" = let buffer = #"Added Index"[Visit start time] in Table.AddColumn(#"Added Index", "Eligible", each if [Visit end time] <> null 
    then Duration.TotalMinutes(
        (buffer{[Index]+1}
        ?? buffer{[Index]+2}) - [Visit end time]) < 60 else null),
    */
        priorStep2 =Table.FillDown(add_eligible_index,{"Visit start date", "Visit end date"}),
        null_index =
            let
                buffer_emp = List.Buffer(priorStep2[#"Emp. #"]), // needed for employee id condition
                buffer_date = List.Buffer(priorStep2[Visit start date]),
                buffer_start = List.Buffer(priorStep2[Visit start time]),
                buffer_code = List.Buffer(priorStep2[Earning Code]),
                buffer_eligible = List.Buffer(priorStep2[Eligible]),
                buffer_end = List.Buffer(priorStep2[Visit end time])
            in
                List.Generate(
                    () => {0,0,{},buffer_date{0}}, // current row, null index, non null list, current date
                    each _{0} < List.Count(buffer_end),
                    each  
                        {
                            _{0}+1, 
                            if 
                                buffer_date{_{0}+1} <> _{3} 
                                or buffer_emp{_{0}+1} <> buffer_emp{_{0}}
                            then List.Count(_{2})
                            else if buffer_code{_{0}} = "TTBV" then _{1} + 1 else _{1},
                            if 
                                try buffer_code{_{0}} <> "TTBV"
                                and (buffer_eligible{_{0}} = true and buffer_eligible{_{0}} <> null)
                                otherwise false 
                            then List.Buffer(_{2} & {_{0}}) 
                            else List.Buffer(_{2}),
                            buffer_date{_{0}+1}
                        }, // create list of all non null values.  create index to select from non null values.  increase index when null value is found or take last non null value when visit start date changes so you can pick the first non null value after the date change.
                    each if buffer_code{_{0}} <> "TTBV" then null else try buffer_end{_{2}{_{1}}} otherwise null
                ),
        add_null_index = 
            Table.Buffer(
                Table.FromColumns(
                    Table.ToColumns(priorStep2)&{null_index},
                    Table.ColumnNames(priorStep2)&{"replace_start"}
                )
            ),
        #"Changed Type" = Table.TransformColumnTypes(add_null_index,{{"replace_start", type time}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Visit start time],each [replace_start] ?? [Visit start time],(origV,oldV,newV) as time => newV,{"Visit start time"}),
        #"Added Custom" = Table.AddColumn(#"Replaced Value", "end_replace", each [Visit end time] ?? 
    Time.From(
        Number.Mod(
            Number.From([Visit start time]) 
            + Number.RoundDown([Quantity],0)/(24*60) 
            + Number.Mod([Quantity],1)*100/(24*60*60)
        ,1)
    ), type time),
        #"Replaced Value1" = Table.ReplaceValue(#"Added Custom",each [Visit end time],each [end_replace],(origV,oldV,newV) as time => newV,{"Visit end time"})
    in
        #"Replaced Value1"

     

     

    https://docs.google.com/spreadsheets/d/1Ym6APkXeS0gyx_PxSMmCSM8J8hVNQqDj/edit?usp=sharing&ouid=114317101938718300746&rtpof=true&sd=true

     

18 Replies

  • Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot). Leave out anything not related to the issue.
    If you are unsure how to do that please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
    Please show the expected outcome based on the sample data you provided.

    If you want to get answers faster please refer to https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

  • Adding sample table with desired outputs in red

     

    17/11/202317/11/20238:00:00 AM10:00:00 AMCS1CSW2
    17/11/202317/11/202310:15:00 AM11:15:00 AMCS1CSW1
    17/11/202317/11/202310:00:00 AM10:10:43 AMTTBVTTBV10.43
    17/11/202317/11/202311:30:00 AM1:30:00 PMCS1CSW2
    17/11/202317/11/202311:15:00 AM11:38:18 AMTTBVTTBV23.18
    17/11/202317/11/20231:45:00 PM3:15:00 PMCS1CSW1.5
    17/11/202317/11/20231:30:00 PM1:40:10 PMTTBVTTBV10.1
    17/11/202317/11/20233:15:00 PM4:45:00 PMCS1CSW1.5
    13/11/202313/11/20239:00:00 AM10:00:00 AMCS1CSW1
    13/11/202313/11/202310:30:00 AM12:30:00 PMCS1CSW2
    13/11/202313/11/202310:00:00 AM10:04:97 AMTTBVTTBV4.97
  • What does your Quantity column represent?  Minutes and Seconds or Minutes and Minute Fractions?  10:04:97 AM is not a valid timestamp.

    • James_Galis1's avatar
      James_Galis1
      Helper II

      Hi

       

      The quantity column represents "minutes and seconds".

       

       

    • James_Galis1's avatar
      James_Galis1
      Helper II

      I might have to look at an alternate solution for my second condition.

       

      However, if the first condition can acheived that would be a great help

  • An simple question in the context of DAX. Let's wait and see what you'll get in PQ.

  • edited and updated M code.  I do not understand the >= 60 minute condition.

     

     

    let
        Source = Excel.CurrentWorkbook(){[Name="pay_test"]}[Content],
        changeType = Table.TransformColumnTypes(Source,{{"Emp. #", type text}, {"Employment Type", type text}, {"Type", type text}, {"Visit ID", Int64.Type}, {"Employee Cost Centre", type any}, {"Cost Centre Description", type any}, {"Visit start date", type text}, {"Visit end date", type text}, {"Visit start time", type time}, {"Visit end time", type time}, {"Service type", type text}, {"Earning Code", type text}, {"Quantity", type number}, {"Premium Quantity", type any}, {"Units", type text}, {"Override rate", Int64.Type}, {"Pay amount", Int64.Type}}),
        priorStep = Table.FillDown(changeType,{"Visit start date", "Visit end date"}),
        null_index =
            let
                buffer_date = List.Buffer(priorStep[Visit start date]),
                buffer_time = List.Buffer(priorStep[Visit end time])
            in
                List.Generate(
                    () => {0,0,{},buffer_date{0}}, // current row, null index, non null list, current date
                    each _{0} < List.Count(buffer_time),
                    each  
                        {
                            _{0}+1, 
                            if buffer_date{_{0}+1} <> _{3} then List.Count(_{2}) + Byte.From(buffer_time{_{0}} <> null)
                            else if buffer_time{_{0}} <> null then _{1} 
                            else _{1} + 1,
                            if buffer_time{_{0}} <> null then List.Buffer(_{2} & {_{0}}) else List.Buffer(_{2}),
                            buffer_date{_{0}+1}
                        }, // create list of all non null values.  create index to select from non null values.  increase index when null value is found or take last non null value when visit start date changes so you can pick the first non null value after the date change.
                    each if buffer_time{_{0}} <> null then null else buffer_time{_{2}{_{1}}}
                ),
        add_null_index = 
            Table.Buffer(
                Table.FromColumns(
                    Table.ToColumns(priorStep)&{null_index},
                    Table.ColumnNames(priorStep)&{"replace_start"}
                )
            ),
        #"Changed Type" = Table.TransformColumnTypes(add_null_index,{{"replace_start", type time}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type",each [Visit start time],each [replace_start] ?? [Visit start time],(origV,oldV,newV) as time => newV,{"Visit start time"}),
        #"Added Custom" = Table.AddColumn(#"Replaced Value", "end_replace", each [Visit end time] ?? 
    Time.From(
        Number.Mod(
            Number.From([Visit start time]) 
            + Number.RoundDown([Quantity],0)/(24*60) 
            + Number.Mod([Quantity],1)*100/(24*60*60)
        ,1)
    ), type time),
        #"Replaced Value1" = Table.ReplaceValue(#"Added Custom",each [Visit end time],each [end_replace],(origV,oldV,newV) as time => newV,{"Visit end time"})
    in #"Replaced Value1"

     

     

     

     

     

     

     

     

    • James_Galis1's avatar
      James_Galis1
      Helper II

      Regarding the 60 minute condition, i'll use the example below taken from the attached file


       

      There are 3 visits on this particular day

       

      Visit 1: 9:00 - 10:00

      Visit 2: 11:30 - 12:30

      Visit 3: 13:00 - 15:00

       

      From the above scenario there are 2 gaps in time

       

      Visit 1 to Visit 2 = 90 minutes

      Visit 2 to Visit 3 = 30 minutes

       

      The TTBV is a condition that allows a worker to paid whilst travelling to their next job but only if the gap between the next job is less than 60 minutes so in the example above the TTBV condition should trigger between visit 2 and visit 3 which would mean the start time for the TTBV would be:

       

      12:30pm 

      Hope that makes sense

       

       

      • spinfuzer's avatar
        spinfuzer
        Solution Sage

        Do we fill in the dates for service type "Reimburse KMs Travelled"?  Can I only take end time's from Service Type = "CSW"?