Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
Dear all
I have the following data
Date | Product | Line # |
1-Jan-21 | Product A | Line 1 |
2-Jan-21 | Product A | Line 1 |
3-Jan-21 | Product A | Line 1 |
4-Jan-21 | Product A | Line 1 |
5-Jan-21 | Product A | Line 3 |
6-Jan-21 | Product A | Line 3 |
7-Jan-21 | Product A | Line 3 |
8-Jan-21 | Product C | Line 1 |
9-Jan-21 | Product C | Line 1 |
10-Jan-21 | Product C | Line 1 |
How can I calculate the duration with dax like that:
Product | Duration | From | To | Line # |
Product A | 4 | 1-Jan-21 | 4-Jan-21 | Line 1 |
Product A | 3 | 5-Jan-21 | 7-Jan-21 | Line 3 |
Product C | 3 | 8-Jan-21 | 10-Jan-21 | Line 1 |
@amrabousamra
You can easily do it in Power Query as well.
Select Product and Line then Group as follows:
Create a blank Query, go to the Advanced Editor, clear the existing code, and paste the codes give below and follow the steps.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtT1SszTNTJU0lEKKMpPKU0uUXAEsn0y81IVDJVidaKVjAiqMCaowoSgClM8KozBKswIqjAnqMICU4UzqjssCaowNMCvJBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Product = _t, #"Line #" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Product", "Line #"}, {{"Start Date", each List.Min([Date]), type nullable date}, {"End Date", each List.Max([Date]), type nullable date}, {"Days", each Table.RowCount(_), Int64.Type}})
in
#"Grouped Rows"
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
11 | |
10 | |
10 | |
9 |
User | Count |
---|---|
18 | |
13 | |
12 | |
11 | |
8 |