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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
andynedine
Advocate I
Advocate I

How to get group of values by year and month

Hi!!

I have a table with this kind of values:

YearMonthValue
2018112
2018123
2019103
2019114
2019125
2020101
2020114
2020125
2020122
202113
202126
202132
202131
202135

 

I can't find a solution to make a "sub-table" or filter it to get only that values are from a year and the last month of that year. The final result must be:

YearMonthValue
2018123
2019125
2020125
2020122
202132
202131
202135

 

I got the table with de last values, but without other years (only from 2021):

 

LastValues = 
var table = FILTER(
    RTA,
    AND( 
        MAX('Company'[Year])='Company'[Year],
        MAX('Company'[Month.Number])='Company'[Month.Number]
    )
)
return SUMMARIZE(table, Company[Year], Company[Month.Number], Company[Dept], Company[Nº Dept], Company[Nº Empl])

 

How I would be able to keep the last month of the rest of years??

1 ACCEPTED SOLUTION
andynedine
Advocate I
Advocate I

Finally, I solved my problem with this code:

Summary = 
var tabla2 = FILTER(
    Table,
    IF('Table'[Year]=YEAR(NOW()),
    'Table'[Month.Number]=calculate(MAX(Table[Month.Number]),FILTER(Table,'Table'[Year]=YEAR(NOW()))),
    MAX('Table'[Month.Number])='Table'[Month.Number])
)
return SUMMARIZE(tabla2, Table[Year], Table[Month.Number], Table[Dept], Table[Nº Dept], Table[Nº Empl])

Althouth I could find this one too:

Summary = 
VAR vYear =
    VALUES ( Table[Año] )
VAR vYearMaxMonth =
    ADDCOLUMNS ( vYear, "@MaxMonth", CALCULATE ( MAX ( Table[Month.Number] ) ) )
VAR vYearMaxFilter =
    TREATAS ( vYearMaxMonth, Table[Year], Table[Month.Number] )
VAR vResult =
    CALCULATETABLE ( Table, vYearMaxFilter )
RETURN
    vResult

View solution in original post

4 REPLIES 4
andynedine
Advocate I
Advocate I

Finally, I solved my problem with this code:

Summary = 
var tabla2 = FILTER(
    Table,
    IF('Table'[Year]=YEAR(NOW()),
    'Table'[Month.Number]=calculate(MAX(Table[Month.Number]),FILTER(Table,'Table'[Year]=YEAR(NOW()))),
    MAX('Table'[Month.Number])='Table'[Month.Number])
)
return SUMMARIZE(tabla2, Table[Year], Table[Month.Number], Table[Dept], Table[Nº Dept], Table[Nº Empl])

Althouth I could find this one too:

Summary = 
VAR vYear =
    VALUES ( Table[Año] )
VAR vYearMaxMonth =
    ADDCOLUMNS ( vYear, "@MaxMonth", CALCULATE ( MAX ( Table[Month.Number] ) ) )
VAR vYearMaxFilter =
    TREATAS ( vYearMaxMonth, Table[Year], Table[Month.Number] )
VAR vResult =
    CALCULATETABLE ( Table, vYearMaxFilter )
RETURN
    vResult
Jihwan_Kim
Super User
Super User

Hi @andynedine 

 

Please try the below, and please let me know if it is not working.

 

Outcome =
VAR maxmonthnumber = CALCULATE( max( 'Table'[Month]), ALLEXCEPT( 'Table', 'Table'[Year]))
RETURN
CALCULATE( SELECTEDVALUE('Table'[Value]), 'Table'[Month] = maxmonthnumber)
 
Did I answer your question? Mark my post as a solution!
Appreciate your Kudos!!

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thanks a lot for your answer, but, unfortunately, it returns an error like "this table is not a valid expression" ("La expresión especificada en la consulta no es una expresión de tabla válida." because I use spanish version).

I tried to modify your answer:

 

 

 

Outcome = 
var maxmonthnumber = CALCULATE( max( 'Table'[Month.Number]), ALLEXCEPT( 'Table', 'Table'[Year]))

var tabla = FILTER(
    RTA,
    CALCULATE( SELECTEDVALUE('Table'[Nº Dept]), 'Table'[Month.Number] = maxmonthnumber)
)

return SUMMARIZE(tabla, Table[Year], Table[Month.Number], Table[Company], Table[Nº Dept], Table[Nº.Empl])

 

 

 

But I got correct values.... less 2021.

YearMonthValue
2018123
2019125
2020125
2020122

Try to create a new table like this:

Table 2 = 
var current_year = YEAR(TODAY())
var max_month = 12
var not_c_y = FILTER('Table','Table'[Year]<>current_year&&'Table'[Month]=max_month)
var c_y = FILTER('Table','Table'[Year]=current_year)
return UNION(c_y,not_c_y)

 

V-lianl-msft_0-1616999373247.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.