Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Calculated column - crate ID number

Hi i am trying to crate 2 Calculated columns:

 

1) Sprint ID 

2) Sprint Time - start from 0 untill velocity is less the 15

 

i tried this formula but no luck....

 

Sprint ID =
IF(Data[Helper]=1,
CALCULATE(COUNTROWS(Data),
FILTER(Data,
Data[Index] <= EARLIER(Data[Index]) && Data[Date]=EARLIER(Data[Date]) )) ,BLANK())

 

DateTimeSecondsVelocityAccelerationIndexSprint TimeSprint ID
02/03/201918:19:00313.510.010  
02/03/201918:19:00313.510.011  
02/03/201918:19:00313.740.172  
02/03/201918:19:00414.060.353  
02/03/201918:19:00413.630.074  
02/03/201918:19:00413.840.25  
02/03/201918:19:00413.980.246  
02/03/201918:19:00414.360.457  
02/03/201918:19:00414.820.658  
02/03/201918:19:00415.160.73901
02/03/201918:19:00415.390.711011
02/03/201918:19:00515.460.581121
02/03/201918:19:00515.460.581231
02/03/201918:19:00515.810.671341
02/03/201918:19:00516.040.671451
02/03/201918:19:00516.290.681561
02/03/201918:20:00517.10.871671
02/03/201918:20:00517.10.871781
02/03/201918:20:00517.060.621891
02/03/201918:20:00517.490.7719101
02/03/201918:20:00517.690.7120111
02/03/201918:20:00617.670.5221121
02/03/201918:20:00617.620.3622131
02/03/201918:20:00617.540.2123141
02/03/201918:20:00617.730.2924151
02/03/201918:20:00617.440.0225161
02/03/201918:20:00617.520.0826171
02/03/201918:20:00617.450.0127181
02/03/201918:20:001110.160.5628  
02/03/201918:20:001210.150.3229  
02/03/201918:20:001210.290.3430  
02/03/201918:20:001210.80.6131  
02/03/201918:20:001210.560.2932  
02/03/201918:20:001210.180.0133  
02/03/201918:20:001210.70.3634  
02/03/201918:20:001310.790.3435  
02/03/201918:20:001314.712.0799  
02/03/201918:20:001315.782.310002
02/03/201918:20:001316.392.1410112
02/03/201918:20:001317.581.9110222
02/03/201918:20:001317.581.9110332
02/03/201918:20:001317.861.6210442
02/03/201918:20:001318.561.710552
02/03/201918:20:001419.141.6810662
02/03/201918:20:001419.451.4810772
02/03/201918:20:001420.081.5410882
02/03/201918:20:001420.191.2410992
02/03/201918:20:001420.771.33110102
02/03/201918:20:001421.071.2111112

 

 

  • ImkeF's avatar
    ImkeF
    7 years ago

    Hi BekahLoSurdo ,

    that's a good solution to the problem.

    However, if you apply it to large data, it could could become slow. This will probably run much faster and is also a bit shorter:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZZdlsIwCEb30mdPhJC/uhWP+9/GNHxEnZ4M6bxYq70mcIH4fG4U7yT3SLxvt43bg/cH0fFW+q2EzMeVAulle93+BfAVoCYFuB6X6AL9QU6BigKS9TeWgIQi2FLVjy4ADVuKx2u+8vze8Hy/LVdCEISQegj1CtCiAqUDbQ3kwFih9tD3K4DsANSbbzqDSFgi9+DZVz0jfNdGNFRTqZroNVECpS/Ct21ERORFdzX3HemLqAGbarrE3LcDzH2fAKvxEvXbK0Qyf7rG3PiJKF/G49z4IIoRFf76ruLc+JlA3Wq9x7nxE5Gt93RXc+MnoqK9VWOcGz8RCWuQxuEbH7tCHNRrJPrKxxp5e8/B6Dtn9Nzo2azJ8qVzNASriEbiWx+Ilbv0HIivfSAYb6VvU3ztg8jl40R87+9Q2idh4osfSP0Ul/jiWYz4Dt43DyRZf+D02BcpFkytqmUSsOYixYIhpOM3BtZpTIskixWlTqywo3wWWZ4yizQb07SkbRjRlUQ3FACrIaZFojXm3WK3OUx+kw1Gu4xDArNos6SDDk3MGDRMiz4zBp/jhGdaVIExGMVBNCN/HKm/GUaVsf7z4H6mvn4A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Time = _t, Seconds = _t, Velocity = _t, Acceleration = _t, Index = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Time", type time}, {"Seconds", Int64.Type}, {"Velocity", type number}, {"Acceleration", type number}, {"Index", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "GreaterThan15", each [Velocity] > 15),
        #"Grouped Rows" = Table.Group(#"Added Custom", {"GreaterThan15"}, {{"All", each if List.First(_[GreaterThan15]) = true then Table.AddIndexColumn(_, "SprintTime",0,1) else _}}, GroupKind.Local),
        #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index1", 0, 1),
        #"Expanded All" = Table.ExpandTableColumn(#"Added Index", "All", {"Date", "Time", "Seconds", "Velocity", "Acceleration", "Index", "SprintTime"}, {"Date", "Time", "Seconds", "Velocity", "Acceleration", "Index", "SprintTime"}),
        AddSprintID = Table.AddColumn(#"Expanded All", "SprintID", each if [GreaterThan15] then Number.RoundUp([Index1]/2) else null),
        #"Removed Columns" = Table.RemoveColumns(AddSprintID,{"GreaterThan15", "Index1"})
    in
        #"Removed Columns"

    The key lies in the 4th parameter of the Table.Group-function, where you use GroupKind.Local. See this article on how it works: https://blog.crossjoin.co.uk/2014/01/03/aggregating-by-local-groups-in-power-query/ 

6 Replies

  • vanessafvg's avatar
    vanessafvg
    Community Champion

    Anonymous  i dont feel like you have explained this clearly, you already have a sprint id, what sprint id are you trying to calcualted.

     

    sprint time?   are you wanting to sum all the time up until velocity < 15

     

    also in your calculation you have helper?  Data[Helper]=1

     

    where is that in this data? 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi sorry i was unclear, the table i attached was my dasire outcome what i have at the moment is 

       

       

      Date   Time     Seconds   Velocity   Accelartion   

       

      DateTimeSecondsVelocityAcceleration
      02/03/201918:19:00313.510.01
      02/03/201918:19:00313.510.01
      02/03/201918:19:00313.740.17
      02/03/201918:19:00414.060.35
      02/03/201918:19:00413.630.07
      02/03/201918:19:00413.840.2
      02/03/201918:19:00413.980.24
      02/03/201918:19:00414.360.45
      02/03/201918:19:00414.820.65
      02/03/201918:19:00415.160.73
      02/03/201918:19:00415.390.71
      02/03/201918:19:00515.460.58
      02/03/201918:19:00515.460.58
      02/03/201918:19:00515.810.67
      02/03/201918:19:00516.040.67
      02/03/201918:19:00516.290.68
      02/03/201918:20:00517.10.87
      02/03/201918:20:00517.10.87
      02/03/201918:20:00517.060.62
      02/03/201918:20:00517.490.77
      02/03/201918:20:00517.690.71
      02/03/201918:20:00617.670.52
      02/03/201918:20:00617.620.36
      02/03/201918:20:00617.540.21
      02/03/201918:20:00617.730.29
      02/03/201918:20:00617.440.02
      02/03/201918:20:00617.520.08
      02/03/201918:20:00617.450.01
      02/03/201918:20:001110.160.56
      02/03/201918:20:001210.150.32
      02/03/201918:20:001210.290.34
      02/03/201918:20:001210.80.61

       

      • BekahLoSurdo's avatar
        BekahLoSurdo
        Resolver IV

        Hi Anonymous,

         

        I would do this in Power Query. Using your starting data in an Excel workbook, here is my code:

        let
            Source = Excel.Workbook(File.Contents("C:\Users\rlosurdo\Desktop\SprintTime.xlsx"), null, true),
            Original_Sheet = Source{[Item="Original",Kind="Sheet"]}[Data],
            #"Promoted Headers" = Table.PromoteHeaders(Original_Sheet, [PromoteAllScalars=true]),
            #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Time", type datetime}, {"Seconds", Int64.Type}, {"Velocity", type number}, {"Acceleration", type number}}),
            #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
            Velocities = Table.FromColumns(
        	Table.ToColumns(#"Added Index") & 
        	Table.ToColumns(
        		Table.InsertRows(Table.SelectColumns(#"Added Index","Velocity"),0,{[Velocity = null]})) &
        	Table.ToColumns(
        		Table.RemoveFirstN(Table.SelectColumns(#"Added Index","Velocity"),1)),
        	Table.ColumnNames(#"Added Index") & {"PreviousVelocity", "NextVelocity"}),
            #"Removed Bottom Rows" = Table.RemoveLastN(Velocities,1),
            #"Added SprintAudit" = Table.AddColumn(#"Removed Bottom Rows", "SprintAudit", each if [Velocity] >= 15 then if [PreviousVelocity] < 15 then "Initial" else if [NextVelocity] < 15 or [NextVelocity] is null then "Completion" else "Continued" else null),
            #"Added Join Key 1" = Table.AddColumn(#"Added SprintAudit", "Join Key", each if [SprintAudit] <> null then 1 else 0),
            InitialIndexTable = Table.SelectRows(#"Added Join Key 1", each ([SprintAudit] = "Initial")),
            #"Added Initial Index" = Table.AddIndexColumn(InitialIndexTable, "Sprint Index", 1, 1),
            CompletionIndexTable = Table.SelectRows(#"Added Join Key 1", each ([SprintAudit] = "Completion")),
            #"Added Completion Index" = Table.AddIndexColumn(CompletionIndexTable, "Sprint Index", 1, 1),
            IndexTable = Table.Combine({#"Added Completion Index", #"Added Initial Index"}),
            #"Removed Other Columns" = Table.SelectColumns(IndexTable,{"SprintAudit", "Sprint Index", "Index"}),
            #"Pivoted Column" = Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[SprintAudit]), "SprintAudit", "Index", List.Sum),
            #"Added Join Key" = Table.AddColumn(#"Pivoted Column", "Join Key", each 1),
            #"Merged Queries" = Table.NestedJoin(#"Added Join Key 1",{"Join Key"},#"Added Join Key",{"Join Key"},"IndexTable",JoinKind.LeftOuter),
            #"Expanded IndexTable" = Table.ExpandTableColumn(#"Merged Queries", "IndexTable", {"Sprint Index", "Completion", "Initial"}, {"Sprint Index", "Completion", "Initial"}),
            #"Added Sprint ID" = Table.AddColumn(#"Expanded IndexTable", "Sprint ID", each try if [Index] >= [Initial] and [Index] <= [Completion] then [Sprint Index] else 0 otherwise null),
            #"Filtered Rows" = Table.SelectRows(#"Added Sprint ID", each ([Sprint ID] <> 0)),
            Partition = Table.Group(#"Filtered Rows", {"Sprint ID"}, {{"Partition", each Table.AddIndexColumn(_, "Sprint Time Setup",0,1), type table}}),
            #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Date", "Time", "Seconds", "Velocity", "Acceleration", "Index", "Sprint Time Setup"}, {"Date", "Time", "Seconds", "Velocity", "Acceleration", "Index", "Sprint Time Setup"}),
            #"Added Sprint Time" = Table.AddColumn(#"Expanded Partition", "Sprint Time", each if [Sprint ID] <> null then [Sprint Time Setup] else null),
            #"Sorted Rows" = Table.Sort(#"Added Sprint Time",{{"Index", Order.Ascending}}),
            #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Sprint Time Setup"}),
            #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Date", "Time", "Seconds", "Velocity", "Acceleration", "Index", "Sprint Time", "Sprint ID"})
        in
            #"Reordered Columns"

        Here is the resulting table in Power BI:

         

        I basically created a few temporary tables so that I could define and index each sprint as its own entity. Then I used ImkeF's partition magic (https://community.powerbi.com/t5/Desktop/Custom-column-Index-or-Ranking-by-other-column/td-p/33864) to add an index for each individual Sprint ID.

         

        Let me know if you have any questions or would like me to send over my workbook.