Skip to content

Instantly share code, notes, and snippets.

@PBI-DataVizzle
Created June 1, 2022 20:51
Show Gist options
  • Select an option

  • Save PBI-DataVizzle/0ca471585a69223c1393a376917995c9 to your computer and use it in GitHub Desktop.

Select an option

Save PBI-DataVizzle/0ca471585a69223c1393a376917995c9 to your computer and use it in GitHub Desktop.

Function: Convert Fiscal Period to Month Number

fnFiscalPeriodToMonth

let
  fn =  // fnFiscalPeriodToMonth
/* ------------------------------ 
  Author: Imran Haq - PBI QUERYOUS
  GitHub: https://github.com/PBIQueryous/M-Code/
  Description: Takes Fiscal Period and Converts to Month Number
  Credits: Gilbert Quevauvilliers
  Link: https://gqbi.wordpress.com/2016/09/07/create-dynamic-periods-for-fiscal-or-calendar-dates-in-power-bi/
  Site: https://gqbi.wordpress.com/
 ---------------------------------*/

// invoke function & define parameter inputs
    let
      invokeFn = (fiscalMonthNumber as number, dateColumn as any) =>
        
// ------------------------------------------------------------------
// function transformations
        let
      fiscalMonthStart = fiscalMonthNumber,
      fiscalMonthCalc = 12 - fiscalMonthStart,
      calculation = Number.Mod(dateColumn + fiscalMonthCalc , 12)+1, 
      Result = calculation
    in
     Result
     , 

// ------------------------------------------------------------------     
// change parameter metadata here
      fnType = type function (
        exampleInput as (
          type number
            meta 
            [
              Documentation.FieldCaption     = " Fiscal Month Number ", 
              Documentation.FieldDescription = " Input Fiscal Month Start Number ",
              Documentation.SampleValues = {"123"}
            ]
        ),
        column1 as (
          type any
            meta 
            [
              Documentation.FieldCaption     = " Select Fiscal Period Column ", 
              Documentation.FieldDescription = " Select Fiscal Period Column (eg: [Column1]) ",
              Documentation.SampleValues = {"[Column1]"}
            ]
        )
      ) as list,
// ------------------------------------------------------------------
// edit function metadata here
      documentation = 
      [  

          Documentation.Name = " fnFiscalPeriodToMonth ", 
          Documentation.Description = " Takes Fiscal Period and Converts to Month Number ", 
          Documentation.LongDescription = " Takes Fiscal Period and Converts to Month Number ", 
          Documentation.Category = " Fiscal Date ", 
          Documentation.Source = "  PBIQUERYOUS  ", 
          Documentation.Version = " 1.0 ", 
          Documentation.Author = " Imran Haq ", 
          Documentation.Examples = 
          {
            [
            Description = "  ExampleDescription   ",
            Code    = "    = Number.Mod(dateColumn + fiscalMonthCalc , 12)+1     ", 
            Result  = "    ExampleOutput
                      #(lf) 
                      #(lf) 
                      "
            ]
          }
       
      ]
       ,
       
// ------------------------------------------------------------------
// Choose between Parameter Documentation or Function Documentation
      funtionDocumentation =      // -- function metadata
      Value.ReplaceType(invokeFn, Value.ReplaceMetadata(Value.Type(invokeFn), documentation)),
      
      parameterDocumentation =    // -- parameter metadata
      Value.ReplaceType(invokeFn, fnType) 
    in
// ------------------------------------------------------------------
// select one of the above steps and paste below
      parameterDocumentation      /* <-- Choose final documentation type */
in
  fn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment