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
Anonymous
Not applicable

Latest Common Month in a Country considering Category

I am trying to create one custom column(Latest Common Month in a Country) for flag in in Power query .On the basis Country ,Category ,Year , Month No. I am trying to find the Latest month in a country Flag. The data looks like below:

 

paint2.png

 

 

CountryCategoryYearMonth NoLatest Common Month flag in a Country considering CategoryLatest common month flag availbale  in all the country without considering Category
USToothpaste2021811
USSports2021900
USSports2021811
USSports2020800
USToothpaste20191000
USSports20191000
USSports20181000
UKToothpaste2021811
UKSports2021811
UKSports2021900
UKToothpaste20201200
UKSports20201200
UKToothpaste20191000
UKSports20191000
AusToothpaste2021910
AusSports2021910
AusToothpaste2021801
AusSports20201200
AusToothpaste20191000
AusSports20191000
RUSToothpaste2021811
RUSSports2021811

 

 

Please help me to find the latest common month in each country considering Category and  latest common month in all the countries without considering category column

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

5 REPLIES 5
ronrsnfld
Super User
Super User

See the latest version of my answer to your identical question at https://stackoverflow.com/questions/69572644/latest-common-month-in-a-country-considering-category 

v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can try this query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZK9DoAgDITfhdmhZZLR2c2fyTg4mLhhBN9fNERNLZbhQghfrtylw6D6VhWqs9Yv6+T8HC4aNIajDMJLYxGxdrWbdw9iguBSEhFcICLEhfwHz0EIv9OymJJh6rz8tRyOQ4w87HxG/eVoTxyTUxTx4phqd4kWTIz45riMlEk1CnddXy8mJGuWTiDEbDKXvZFWeTwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Country = _t, Category = _t, Year = _t, #"Month No" = _t, #"Latest Common Month flag in a Country considering Category" = _t, #"Latest common month flag availbale  in all the country without considering Category" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Category", type text}, {"Year", Int64.Type}, {"Month No", Int64.Type}, {"Latest Common Month flag in a Country considering Category", Int64.Type}, {"Latest common month flag availbale  in all the country without considering Category", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Country", "Category", "Year"}, {{"Data", each _, type table [Country=nullable text, Category=nullable text, Year=nullable number, Month No=nullable number, Latest Common Month flag in a Country considering Category=nullable number, Latest common month flag availbale  in all the country without considering Category=nullable number]}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Year] = List.Max(#"Grouped Rows"[Year]))),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Month No", each Table.Max([Data],"Month No")[Month No],Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
    #"Grouped Rows1" = Table.Group(#"Removed Columns", {"Country"}, {{"Data", each _, type table [Country=nullable text, Category=nullable text, Year=nullable number, Month No=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows1", "Min Month", each Table.Min([Data],"Month No")[Month No],Int64.Type),
    #"Expanded Data" = Table.ExpandTableColumn(#"Added Custom1", "Data", {"Category", "Year", "Month No"}, {"Category", "Year", "Month No"}),
    #"Filtered Rows1" = Table.SelectRows(#"Expanded Data", each ([Month No] = [Min Month])),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Min Month"})
in
    #"Removed Columns1"

vyingjl_0-1635148698954.png

 

Best Regards,
Community Support Team _ Yingjie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

smpa01
Super User
Super User

@Anonymous  the latest common month by US-2021 is 8 for both Toothpaste and Sports. WHy do you have 8 for Toothpaste and 9 for Sports?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
lbendlin
Super User
Super User

you can do that with a couple of groupings in Power Query.  Please provide sanitized sample data that fully covers your issue. Paste the data into a table in your post or use one of the file services.

Anonymous
Not applicable

pasted as table ..thanks

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.

Top Solution Authors