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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Visualize ages

Ive done some searching and cant quite find the answer to this question.

 

i have a large table. One of the fields has a "first found" date with is a date in the past.  Could be 1 day ago could 1000 days ago.  I have duplciated this column and converted it to age so i have a column shwoing the age which is essnetially thenumber of days between the "first found" and today.  Age, simple.

 

Now i want to visualsie on a single graph (probably just a bar graph) a total value for the following

 

30-60 days old

60-180 days old

180-365 days old

365+ days old

 

I can do this with filters simple enough if i do seperate visuals but can tfigure out how to do it on one visual.  Any ideas?

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous 

 

Download example PBIX file with all the data and charts in this answer.

 

I did this in Power Query but you could also do it in DAX if you like.  I find PQ a bit easier for this.

 

This is the table I end up with

 

age-table.png

 

Here's the query code

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJSitUhgmOMjWOMzDFF5lhSlWNoQCbPECsvFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"First Found" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"First Found", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Age", each Duration.TotalDays(Date.From(DateTime.LocalNow()) - [First Found])),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Age", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Age Bands", each if [Age] < 30 then "1-29" else 

if [Age] < 60 then "30-59" else 

if [Age] < 180 then "60-179" else 

if [Age] < 365 then "180-364" else 

"365+"),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom1", {"Age Bands"}, #"Sort Order", {"Age Band"}, "Sort Order", JoinKind.LeftOuter),
    #"Expanded Sort Order" = Table.ExpandTableColumn(#"Merged Queries", "Sort Order", {"Sort Order"}, {"Sort Order"})
in
    #"Expanded Sort Order"

 

 

What I've done is create Custom Columns for Age and Age bands.  The Sort Order column comes from another table specfically created just so you can sort the Age Bands in a pre-determined way.  If you tried sorting by the Age Bands text values, you'd get undesirable results.  This is the table

 

sort-table.png

 

You'll see that the Age Bands don't overlap, i.e. you can't have bands like 30-60 and 60-180.  A value of 60 can't be in 2 bands.

 

Here's the bar chart

 

age-chart.png

 

It's sorted on the Age Bands but they have a Sort By column which is the Sort Order column - read more on this here Sort a Column with a Custom Order in Power BI - RADACAD

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

1 REPLY 1
PhilipTreacy
Super User
Super User

Hi @Anonymous 

 

Download example PBIX file with all the data and charts in this answer.

 

I did this in Power Query but you could also do it in DAX if you like.  I find PQ a bit easier for this.

 

This is the table I end up with

 

age-table.png

 

Here's the query code

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJSitUhgmOMjWOMzDFF5lhSlWNoQCbPECsvFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"First Found" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"First Found", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Age", each Duration.TotalDays(Date.From(DateTime.LocalNow()) - [First Found])),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Age", Int64.Type}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Age Bands", each if [Age] < 30 then "1-29" else 

if [Age] < 60 then "30-59" else 

if [Age] < 180 then "60-179" else 

if [Age] < 365 then "180-364" else 

"365+"),
    #"Merged Queries" = Table.NestedJoin(#"Added Custom1", {"Age Bands"}, #"Sort Order", {"Age Band"}, "Sort Order", JoinKind.LeftOuter),
    #"Expanded Sort Order" = Table.ExpandTableColumn(#"Merged Queries", "Sort Order", {"Sort Order"}, {"Sort Order"})
in
    #"Expanded Sort Order"

 

 

What I've done is create Custom Columns for Age and Age bands.  The Sort Order column comes from another table specfically created just so you can sort the Age Bands in a pre-determined way.  If you tried sorting by the Age Bands text values, you'd get undesirable results.  This is the table

 

sort-table.png

 

You'll see that the Age Bands don't overlap, i.e. you can't have bands like 30-60 and 60-180.  A value of 60 can't be in 2 bands.

 

Here's the bar chart

 

age-chart.png

 

It's sorted on the Age Bands but they have a Sort By column which is the Sort Order column - read more on this here Sort a Column with a Custom Order in Power BI - RADACAD

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.