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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
greenmonsta
Frequent Visitor

count data across mutiple ranges

Hi All,

I am trying to solve the following in Power Query.

In the data table I have Ticket Number, the date the ticket was created and the date the ticket was resolved. If the resolved cell is blank the ticket is still open. Then the other table is a list of sprints with their start and end dates. For each sprint I am trying to get the count of the tickets that were created, resolved, and remain open. It is the remain open column that I am really struggling with, For example ticket HVSD-19211 needs to be counted as open in sprints 2211, 2212, 2213 and 2214.

tablestables

 

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @greenmonsta ,
didn't get that requirement before.
You can adjust like this:

let
  Source = Sprints, 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Days", 
    each {Number.From([Start]) .. Number.From([End])}
  ), 
  #"Expanded Days" = Table.ExpandListColumn(#"Added Custom", "Days"), 
  #"Changed Type" = Table.TransformColumnTypes(#"Expanded Days", {{"Days", type date}}), 
  Merge = Table.NestedJoin(
    #"Changed Type", 
    {"Days"}, 
    Tickets_Exanded, 
    {"Dates"}, 
    "Starts", 
    JoinKind.LeftOuter
  ),
    Expanded = Table.ExpandTableColumn(Merge, "Starts", {"Ticket Num", "Opened Date", "Closed Date"}, {"Ticket Num", "Opened Date", "Closed Date"}),
    AddOpened = Table.AddColumn(Expanded, "Opened", each if [Days] = [Opened Date] then [Opened Date] else null),
    AddClosed = Table.AddColumn(AddOpened, "Closed", each if [Days] = [Closed Date] then [Closed Date]  else null),
    AddIgnoreForOpen = Table.AddColumn(AddClosed, "IgnoreForOpen", each  [Opened Date] >= [Start] and [Closed Date] <= [End]),
    #"Grouped Rows" = Table.Group(
    AddIgnoreForOpen, 
    {"Sprint", "Start", "End"}, 
    {
      {
        "Tickets Created", 
        each List.Count(List.Distinct(List.Select(_[Opened], each _ <> null))), 
        Int64.Type
      }, 
      {
        "Tickets Closed", 
        each List.Count(List.Distinct(List.Select(_[Closed], each _ <> null))), 
        Int64.Type
      }, 
      {"Tickets Open", each List.Count(List.Distinct(Table.SelectRows(_, (x) => not x[IgnoreForOpen])[Ticket Num])), Int64.Type}
    }
  )
in
  #"Grouped Rows"


Also check enclosed file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

9 REPLIES 9
ImkeF
Community Champion
Community Champion

Hi @greenmonsta ,
didn't get that requirement before.
You can adjust like this:

let
  Source = Sprints, 
  #"Added Custom" = Table.AddColumn(
    Source, 
    "Days", 
    each {Number.From([Start]) .. Number.From([End])}
  ), 
  #"Expanded Days" = Table.ExpandListColumn(#"Added Custom", "Days"), 
  #"Changed Type" = Table.TransformColumnTypes(#"Expanded Days", {{"Days", type date}}), 
  Merge = Table.NestedJoin(
    #"Changed Type", 
    {"Days"}, 
    Tickets_Exanded, 
    {"Dates"}, 
    "Starts", 
    JoinKind.LeftOuter
  ),
    Expanded = Table.ExpandTableColumn(Merge, "Starts", {"Ticket Num", "Opened Date", "Closed Date"}, {"Ticket Num", "Opened Date", "Closed Date"}),
    AddOpened = Table.AddColumn(Expanded, "Opened", each if [Days] = [Opened Date] then [Opened Date] else null),
    AddClosed = Table.AddColumn(AddOpened, "Closed", each if [Days] = [Closed Date] then [Closed Date]  else null),
    AddIgnoreForOpen = Table.AddColumn(AddClosed, "IgnoreForOpen", each  [Opened Date] >= [Start] and [Closed Date] <= [End]),
    #"Grouped Rows" = Table.Group(
    AddIgnoreForOpen, 
    {"Sprint", "Start", "End"}, 
    {
      {
        "Tickets Created", 
        each List.Count(List.Distinct(List.Select(_[Opened], each _ <> null))), 
        Int64.Type
      }, 
      {
        "Tickets Closed", 
        each List.Count(List.Distinct(List.Select(_[Closed], each _ <> null))), 
        Int64.Type
      }, 
      {"Tickets Open", each List.Count(List.Distinct(Table.SelectRows(_, (x) => not x[IgnoreForOpen])[Ticket Num])), Int64.Type}
    }
  )
in
  #"Grouped Rows"


Also check enclosed file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

@ImkeF You are the bomb! Thank you! I leaned a lot here, not only your solution but the your style is awsome! Thanks again.

ImkeF
Community Champion
Community Champion

Hi @greenmonsta ,
I've used Gregs file, so you'll find both our solutions in it.
The Power Query solution is in Table "Result". 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF . Thank you for pointing out the Power Query Result. I see it now. This is so much better than the way I was attacking it. My tables were really wide. This is much more efficient, really great insight. I am not getting the "Tickets Open" column to calculate correctly though. I believe it is taking a distinct count, but if a ticket were opened and closed in the same sprint it should not be counted. Any thoughts on how to solve this?

ImkeF
Community Champion
Community Champion

Hey @greenmonsta ,
please check the file enclosed (edited on July 23rd). 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Hi @ImkeF , Appreciate the feed back. Unless I am missing something(and I very well could be) this is still a Dax solution. I'm not using Powe BI. I am using Power Query to prepare the data for output into an Excel spreadsheet. I think I have a solution though I have not fully varified my results yet. This is executed while in the Sprint date table in Power Query. The big change in thought for me was comapring the Created date to the Sprint end date as oppsed to usint the Sprint start date for comarisions. @Greg_Deckler  I will update this thread once I have had more time to verify my reults.

= List.Count(
Table.SelectRows(
Add_Sprint_End,
(IT) =>
IT[Resolved] > [End Date] and
IT[Created] <= [End Date]
) [Merged]
)

Greg_Deckler
Community Champion
Community Champion

@greenmonsta I don't have a Power Query solution for this but I did create a DAX solution for this a long time ago. 
https://community.powerbi.com/t5/Quick-Measures-Gallery/Open-Tickets/m-p/409364



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hey Greg, Thanks for the fast reponse. I really need it in Power Query but I am looking at the Dax. Maybe I can get some hints out ot there to apply to my M code.

@greenmonsta Yeah, I'm not sure how you would go about that in Power Query. Maybe @ImkeF has some thoughts.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors