Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
PowerWhy
Helper IV
Helper IV

M Code - find prior fiscal period (even if in prior fiscal year)

Hello,

 

I've managed to create this MCode to filter by the previous fiscal period. Please can someone point me in the right direction to write it so that is can cope with looking back to the prievious fiscal year? (e.g. if it is fiscal period 1 now, then it should filter by period 12, and the preiovius fiscal year ("Year - 1").

 

FilteredFYPeriod = Table.SelectRows(Debt_, each [FY] = Year and [Period]=Period - 1 ),

 

Thank you for any help,

 

PW

1 ACCEPTED SOLUTION

Hi, @PowerWhy 

You can try the following M code:

let
Source = PowerPlatform.Dataflows([]),
Workspaces = Source{[Id="Workspaces"]}[Data],
Workspace_to_use = Workspaces{[workspaceId=Workspace_ID]}[Data],
Dataflow_to_use = Workspace_to_use{[dataflowId=Dataflow_ID]}[Data],
Debt_ = Dataflow_to_use{[entity="Debt",version=""]}[Data],
FilteredFYPeriod = Table.SelectRows(Debt_,
    each if Period = 1
    then [FY] = Year - 1 and [Period] = 12 else
    [FY] = Year and [Period] = Period - 1
)
in
FilteredFYPeriod

Hope this helps you.

 

 

How to Get Your Question Answered Quickly

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
PowerWhy
Helper IV
Helper IV

Ahh sorry I'm switching between too many programming languages! 🙂 - I think this is the fix?:

 

let
Source = PowerPlatform.Dataflows([]),
Workspaces = Source{[Id="Workspaces"]}[Data],
Workspace_to_use = Workspaces{[workspaceId=Workspace_ID]}[Data],
Dataflow_to_use = Workspace_to_use{[dataflowId=Dataflow_ID]}[Data],
Debt_ = Dataflow_to_use{[entity="Debt",version=""]}[Data],
FilteredFYPeriod = Table.SelectRows(Debt_,
  if Period = 1 then
    each [FY] = Year - 1 and [Period] = 12 else
    each [FY] = Year and [Period] = Period - 1
)
in
FilteredFYPeriod

Hi, @PowerWhy 

You can try the following M code:

let
Source = PowerPlatform.Dataflows([]),
Workspaces = Source{[Id="Workspaces"]}[Data],
Workspace_to_use = Workspaces{[workspaceId=Workspace_ID]}[Data],
Dataflow_to_use = Workspace_to_use{[dataflowId=Dataflow_ID]}[Data],
Debt_ = Dataflow_to_use{[entity="Debt",version=""]}[Data],
FilteredFYPeriod = Table.SelectRows(Debt_,
    each if Period = 1
    then [FY] = Year - 1 and [Period] = 12 else
    [FY] = Year and [Period] = Period - 1
)
in
FilteredFYPeriod

Hope this helps you.

 

 

How to Get Your Question Answered Quickly

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

PowerWhy
Helper IV
Helper IV

Thank you @v-jianpeng-msft I'm afraid I'm not seeing how to apply that to my situation. Please could you tell me how to adjust the formula below to achieve the same?

let
Source = PowerPlatform.Dataflows([]),
Workspaces = Source{[Id="Workspaces"]}[Data],
Workspace_to_use = Workspaces{[workspaceId=Workspace_ID]}[Data],
Dataflow_to_use = Workspace_to_use{[dataflowId=Weekly_Dataflow_ID]}[Data],
Debt_ = Dataflow_to_use{[entity="Debt",version=""]}[Data],
FilteredFYPeriod = Table.SelectRows(Debt_,
  if(Period = 1,
    each [FY] = Year - 1 and [Period] = 12,
    each [FY] = Year and [Period] = Period - 1
  )
)
in
FilteredFYPeriod

 

Thank you,

 

PW

v-jianpeng-msft
Community Support
Community Support

Hi, @PowerWhy 

You can try the following M code, please adjust according to your table:

let
     Source = YourTable, // Replace with your data table name
     currentPeriod = YourTable[Period], // Replace with your "Period" column name
     currentYear = Date.Year(DateTime.LocalNow()),
     previousYear = currentYear - 1,
     FilteredRows = Table.SelectRows(Source, each [Period] = 12 or [Period] = currentPeriod or [Period] = previousYear)
in
     FilteredRows

 I created the following Sample data:

vjianpengmsft_0-1709097396211.png

I create a new blank query and use the M code above:

vjianpengmsft_1-1709097490822.png

The query results are as follows:

vjianpengmsft_2-1709097506049.png

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.