Forum Discussion

pontushaglund's avatar
7 years ago
Solved

Finding first, second, third occurence

Hi there.   I have this table of data: I need to calculate a penalty for each row where the penalty is increased for conscutive rows: The first row with a value above threshold (Index 1, 5,...
  • LivioLanzo's avatar
    7 years ago

    This could be another option for you:

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTI0MACSlgZKsTrRSkYgASMkAWOQgCmSgAmQaYHEN0U3wgwkYIgkYI5uhAVIwARJwBJsBoIPZqNoMQS71AxZBOxUuEtiAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Index = _t, Value = _t, Threshold = _t]),
        
        ChangedType = Table.TransformColumnTypes(Source,{{"Index", Int64.Type}, {"Value", Int64.Type}, {"Threshold", Int64.Type}}),
        
        AddAboveThreshold = Table.AddColumn( ChangedType, "AboveThreshold", each [Value] > [Threshold], type logical),
        
        Grouping = Table.Group( AddAboveThreshold, {"AboveThreshold"}, {"AllRows", each _, type table}, GroupKind.Local),
        
        ValueMapping = #table( type table [Index3= Number.Type, AddedValue = Number.Type], { {1, 1000}, {2, 2000}, {3, 5000} } ),
        
        fnAddValue = ( tbl as table ) as table =>
            let
                AddIndxCol = Table.AddIndexColumn( tbl, "Index2", 1, 1 ),
                MergeTables = Table.NestedJoin( AddIndxCol, {"Index2"}, ValueMapping, {"Index3"}, "JoinedTable", JoinKind.LeftOuter ),
                ExpandCol = Table.ExpandTableColumn( MergeTables, "JoinedTable", {"AddedValue"} ),
                FillDown = Table.FillDown( ExpandCol, {"AddedValue"})
            in
                Table.SelectColumns(FillDown, {"Index", "Value", "Threshold", "AddedValue"} ),
        
        ModifyTableCell = Table.AddColumn( 
                                    Grouping, 
                                    "AddValue", 
                                    each 
                                        if 
                                            [AboveThreshold] = true 
                                         then 
                                            fnAddValue( [AllRows] ) 
                                        else 
                                            Table.SelectColumns( [AllRows], {"Index", "Value", "Threshold"} ),
                                    type table ),
                                    
        ExpandedAddValue = Table.ExpandTableColumn(
                                ModifyTableCell, 
                                "AddValue", 
                                {"Index", "Value", "Threshold", "AddedValue"}
                          ),
        RemovedOtherColumns = Table.SelectColumns(
                                                 ExpandedAddValue,
                                                 {"Index", "Value", "Threshold", "AddedValue"}),
                                                 
        ChangedType2 = Table.TransformColumnTypes(
                                RemovedOtherColumns,
                                {
                                    {"Index", Int64.Type}, 
                                    {"Value", type number}, 
                                    {"Threshold", type number}, 
                                    {"AddedValue", Int64.Type}
                                }
                            )
    in
        ChangedType2