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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply

Automatically filter month based on real date

Hi guys, I'm currently an finance officer and on of my monthly task is to create a PBI dashboard to present data of company. I want to automate my report so that once I have new data, I just have to refresh and PBI will do everything. Currently, I have a trouble with my task.

My data contain transactions of all month, spread year to year. For the purpose of the report, I only extract two most recent months for calculation and visualization. Data will look like this ( assume name of this table below is Data):

 

DateTransactions
17-10-23A
18-10-23B
19-10-23C
16-11-23D
15-11-23E
19-11-23F
24-12-23G
12-12-23H
23-12-23I

For example, in December, I only take transactions of November and October for reporting. I create a new calculated column name Month, in which is the month number of column date. Then I create a new table with the formula:

Data_report = Filter(Data, Data[Month]=Month(today())||Data[Month] = Month(today())-1)

With this DAX, each time I have to do monthly report, data will be selected for the most 2 recent months.

Usually, this DAX runs well. However, since it is now Jannuary, this dax no longer runs. I realized the problem is my Dax now calculate results will be 0 and -1, not 11 or 12 as I expected. 

Thus, anyone can help me to fix my DAX or create a new one to automate the data processing.

Sorry English is not my first language, so my post is difficult to understand, please leave me a message.

Many thanks

Regards,

2 ACCEPTED SOLUTIONS
jdbuchanan71
Super User
Super User

@Nerdywantocode 

You should be able to do it just using the dates without adding the month to the table like this.

Data_report = 
VAR _Start = EOMONTH(TODAY(),-2)+1
VAR _End = EOMONTH(TODAY(),0)
RETURN 
FILTER ( Data, Data[Date] >= _Start && Data[Date] <= _End )

View solution in original post

v-heq-msft
Community Support
Community Support

Hi @Nerdywantocode ,

Here some steps that I want to share, you can check them if they suitable for your requirement.

Here is my test data:

vheqmsft_0-1704865325622.png

1.Create a new calculated column in  `Data` table to determine the year and month combination for each transaction date.

 

YearMonth = YEAR(Data[Date]) * 100 + MONTH(Data[Date])

 

vheqmsft_2-1704865839345.png

2.Create a measure.

Two months ago = IF(
    MONTH(TODAY()) = 1 || 2,
    (YEAR(TODAY()) - 1) * 100 + 11,
    YEAR(TODAY()) * 100 + MONTH(TODAY()) - 2
)

 

 

3.Create another measure to calculate the  year and last month combination.

 

LastMonth = IF(
    MONTH(TODAY()) = 1 || 2,
    (YEAR(TODAY()) - 1) * 100 + 12,
    YEAR(TODAY()) * 100 + MONTH(TODAY()) - 1
)

 

4.Create a calculate table to show the final output

 

Table = CALCULATETABLE(Data,
    FILTER(
    Data,
    Data[YearMonth] = [Two months ago] || Data[YearMonth] = [LastMonth])
)

 

5.Final output

vheqmsft_0-1705456479280.png

 

Best regards

Albert He

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

3 REPLIES 3
v-heq-msft
Community Support
Community Support

Hi @Nerdywantocode ,

Here some steps that I want to share, you can check them if they suitable for your requirement.

Here is my test data:

vheqmsft_0-1704865325622.png

1.Create a new calculated column in  `Data` table to determine the year and month combination for each transaction date.

 

YearMonth = YEAR(Data[Date]) * 100 + MONTH(Data[Date])

 

vheqmsft_2-1704865839345.png

2.Create a measure.

Two months ago = IF(
    MONTH(TODAY()) = 1 || 2,
    (YEAR(TODAY()) - 1) * 100 + 11,
    YEAR(TODAY()) * 100 + MONTH(TODAY()) - 2
)

 

 

3.Create another measure to calculate the  year and last month combination.

 

LastMonth = IF(
    MONTH(TODAY()) = 1 || 2,
    (YEAR(TODAY()) - 1) * 100 + 12,
    YEAR(TODAY()) * 100 + MONTH(TODAY()) - 1
)

 

4.Create a calculate table to show the final output

 

Table = CALCULATETABLE(Data,
    FILTER(
    Data,
    Data[YearMonth] = [Two months ago] || Data[YearMonth] = [LastMonth])
)

 

5.Final output

vheqmsft_0-1705456479280.png

 

Best regards

Albert He

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

jdbuchanan71
Super User
Super User

@Nerdywantocode 

You should be able to do it just using the dates without adding the month to the table like this.

Data_report = 
VAR _Start = EOMONTH(TODAY(),-2)+1
VAR _End = EOMONTH(TODAY(),0)
RETURN 
FILTER ( Data, Data[Date] >= _Start && Data[Date] <= _End )

Thank you. I will try this later with my data in the company tmr

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.