Forum Discussion
How to add a column which counts up the repeating value in another column?
Thank you very much.
Okay, so the issue was a fairly simple one: you had some dates that were zeroes:
I've added a simple replace step at the start to negate this and tidied up the code overall. You'll need to add your source steps at the top (I think everything up to #"Gefilterte Zeilen"?) but should work perfectly now.
You'll notice that I put your column rename step right at the end of the query - I always recommend to do this so that if you want to change column names in the future it doesn't break your whole query.
let
Source = yourSourceSteps,
repDateZeroToNull = Table.ReplaceValue( yourLastSourceStep ,0,null,Replacer.ReplaceValue,{"Invoice date", "Planned start date", "Planned finish date"}),
chgDateTypesToText = Table.TransformColumnTypes(repDateZeroToNull,{{"Invoice date", type text}, {"Planned start date", type text}, {"Planned finish date", type text}}),
chgDateTypesToDate = Table.TransformColumnTypes(chgDateTypesToText,{{"Invoice date", type date}, {"Planned start date", type date}, {"Planned finish date", type date}}),
addMonthList = Table.AddColumn(chgDateTypesToDate, "monthList", each
if [Planned start date] = null or [Planned finish date] = null then null else
List.Distinct(
List.Transform(
{Number.From([Planned start date]).. Number.From([Planned finish date])},
each Date.StartOfMonth(Date.From(_))
)
)
),
addSplitInvoiceValue = Table.AddColumn(addMonthList, "splitInvoiceValue", each
if [monthList] = null then null else [Line amount local c] / List.Count([monthList])),
expandMonthList = Table.ExpandListColumn(addSplitInvoiceValue, "monthList"),
chgNewColTypes = Table.TransformColumnTypes(expandMonthList,{{"monthList", type date}, {"splitInvoiceValue", type number}}),
renameCols = Table.RenameColumns(chgNewColTypes,{{"Line amount local c", "Net Invoiced Sales"}})
in
renameCols
Regarding limiting the item codes that the split is applied to, there's a couple of ways to go about this. It really depends whether you want the item code list hardcoded into the query, or whether you will want to easily add/remove item codes in future.
To hardcode, you would use this 'addMonthList' step:
addMonthList = Table.AddColumn(chgDateTypesToDate, "monthList", each
if [Planned start date] = null
or [Planned finish date] = null
or not List.Contains({"630", "100", "620"}, [Item number]) then null else
List.Distinct(
List.Transform(
{Number.From([Planned start date]).. Number.From([Planned finish date])},
each Date.StartOfMonth(Date.From(_))
)
)
),
To be able to more easily change the values in future, you can create a separate query which is just a list of [Item number] values to include, then replace the List.Contains section above like this:
//Change this:
List.Contains({"630", "100", "620"}, [Item number])
//to this:
List.Contains( nameOfYourListQuery , [Item number])
Pete