Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fn = (TableWithDateColumn as table, DateValue as date) =>
GetYearMonth = Table.AddColumn(TableWithDateColumn, "JahrMonat", each Date.Year([Datum])*100+ Date.Month([Datum])),
GetListOfYearMonth = GetYearMonth[JahrMonat],
ConvertToTable = Table.FromList(GetListOfYearMonth , Splitter.SplitByNothing(), null, null, ExtraValues.Error),
RenameColumn = Table.RenameColumns(ConvertToTable,{{"Column1", "JahrMonat"}}),
ChangeType = Table.TransformColumnTypes(RenameColumn,{{"JahrMonat", Int64.Type}}),
RemoveDuplicates = Table.Distinct(ChangeType),
AddIndex = Table.AddIndexColumn(RemoveDuplicates, "SequentialMonthNumber", 1, 1),
JoinQueries = Table.NestedJoin(GetYearMonth,{"JahrMonat"},AddIndex,{"JahrMonat"},"AddIndex",JoinKind.LeftOuter),
NextStep = Table.ExpandTableColumn(JoinQueries, "AddIndex", {"SequentialMonthNumber"}, {"SequentialMonthNumber"}),
NextStep2 = Table.RemoveColumns(NextStep, {"JahrMonat"}),
Output = Table.FirstN(Table.SelectRows(NextStep2,
each (Date.Year(DateValue)*100 + Date.Month(DateValue)) = Date.Year(_[Datum])*100 + Date.Month(_[Datum])),1)[SequentialMonthNumber]{0}
fnType = type function(
TableWithDateColumn as (type table meta [Documentation.LongDescription="Dies muss eine Tabelle sein, in welcher die Spalte 'Datum' existiert."]),
DateValue as (type date meta [Documentation.LongDescription="Hierbei handelt es sich um das Feld 'Datum'"])
) as list meta [
Documentation.Name = "fnGetSequentialMonthNumber",
Documentation.LongDescription =
"Diese Funktion erzeugt eine fortlaufende Monatsnummer, für die Nutzung in DAX Time Intelligence-Funktionen",
Documentation.Author = "Lars Schreiber,"
Value.ReplaceType(fn, fnType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment