Forum Discussion

Mic1979's avatar
Mic1979
Post Partisan
1 year ago
Solved

Custom rounding

Dear all   I am using the following custom rounding as I cannot use the standard rules to round numbers for my scope:   RoundedOEMVolumes = Table.AddColumn(Grouped_Table, "OEM_Volumes_Rounded", e...
  • AmiraBedh's avatar
    1 year ago

    If you want to combine your code, you can useTable.AddColumn once and apply the rounding logic to both columns within the same transformation :

    RoundedVolumes = Table.AddColumn(Grouped_Table, "Rounded_Volumes", each
    [
    OEM_Volumes_Rounded = if Number.Mod([OEM_Volumes], 1) >= Rounding_Factor
    then Number.RoundDown([OEM_Volumes]) + 1
    else Number.RoundDown([OEM_Volumes]),
    DISTRIBUTOR_Volumes_Rounded = if Number.Mod([DISTRIBUTOR_Volumes], 1) >= Rounding_Factor
    then Number.RoundDown([DISTRIBUTOR_Volumes]) + 1
    else Number.RoundDown([DISTRIBUTOR_Volumes])
    ],
    type record
    )

     

    If you want to expand this record into separate columns, you can use Table.ExpandRecordColumn:

    ExpandedRoundedVolumes = Table.ExpandRecordColumn(RoundedVolumes, "Rounded_Volumes", {"OEM_Volumes_Rounded", "DISTRIBUTOR_Volumes_Rounded"})

     

    If you want to improve the performance, you can work with lists instead of the entire table  because it avoids you loading the entire table into memory for each operation. 

    oemVolumes = Table.Column(Grouped_Table, "OEM_Volumes")
    distributorVolumes = Table.Column(Grouped_Table, "DISTRIBUTOR_Volumes")
    
    roundValue = (value) => 
        if Number.Mod(value, 1) >= Rounding_Factor 
        then Number.RoundDown(value) + 1 
        else Number.RoundDown(value)
    
    roundedOEMVolumes = List.Transform(oemVolumes, roundValue)
    roundedDistributorVolumes = List.Transform(distributorVolumes, roundValue)
    
    RoundedVolumesTable = Table.FromColumns(
        Table.ToColumns(Grouped_Table) & {roundedOEMVolumes, roundedDistributorVolumes},
        Table.ColumnNames(Grouped_Table) & {"OEM_Volumes_Rounded", "DISTRIBUTOR_Volumes_Rounded"}
    )