• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Count Zero between Startdate and EndDate |Power Query|Excel Forum|My Online Training Hub

You are here: Home / Count Zero between Startdate and EndDate |Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumPower QueryCount Zero between Startdate and En…
sp_PrintTopic sp_TopicIcon
Count Zero between Startdate and EndDate
Avatar
Chris Yap
Member
Members
Level 0
Forum Posts: 162
Member Since:
August 21, 2019
sp_UserOfflineSmall Offline
1
March 15, 2020 - 11:59 am
sp_Permalink sp_Print

Hi,

What is the shortest and effective way to do in PowerQuery ?  I did extract out the day of start and end,  and attempting to compare whether it is greater or less than,  then countifs in native excel,  haven't really figure out the formulae

Can use M codes in Power Query to achieve it ?

Thank you in advance

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
March 15, 2020 - 2:29 pm
sp_Permalink sp_Print

Hi Chris,

Please prepare a clear example of what you are trying to achieve. Looking at your example does not clarify the problem, the first row shows a count of 4, and you have 6 zero values in columns 1-14 (5 if we start from day 4). I guess the columns represents days from the same month as the start /end dates? Can the end date be in a different month than start date?

Avatar
Chris Yap
Member
Members
Level 0
Forum Posts: 162
Member Since:
August 21, 2019
sp_UserOfflineSmall Offline
3
March 15, 2020 - 5:44 pm
sp_Permalink sp_Print

Hi Catalin

Sorry my mistake

Yes,  for Staff A,  only take into account 4/3/20 to 21/3/20 all the Zero,   so the return should be 6 (there are 6 more days to count),   Staff B should be 5 so far (there are 4 more days)

End date can be across till next month

Basically we want to monitor the number zeros a person have in a specific range of dates

Thank you !

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
March 16, 2020 - 5:05 pm
sp_Permalink sp_Print sp_EditHistory

Try this query (remove last column from the existing table):

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Extracted Day" = Table.TransformColumns(Source,{{"Start date", Date.Day, Int64.Type}, {"End date", Date.Day, Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Extracted Day", {"Name", "Start date", "End date"}, "Attribute", "Value"),
#"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each [Attribute]>=[Start date] and [Attribute]<=[End date]),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = true) and ([Value] = 0)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Name"}, {{"Count", each Table.RowCount(_), type number}}),
#"Merged Queries" = Table.NestedJoin(Source, {"Name"}, #"Grouped Rows", {"Name"}, "Grouped Rows", JoinKind.LeftOuter),
#"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Count"}, {"Count"})
in
#"Expanded Grouped Rows"
Avatar
Chris Yap
Member
Members
Level 0
Forum Posts: 162
Member Since:
August 21, 2019
sp_UserOfflineSmall Offline
5
March 16, 2020 - 6:07 pm
sp_Permalink sp_Print

Hi Catalin

Thank you

it works perfectly if all days within a month

can you enlighten me what if the days cross over to next month,  what is the tweaking to your code ?

 

Thank you !

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
March 17, 2020 - 1:16 am
sp_Permalink sp_Print sp_EditHistory

Hi Chris,

Already asked "Can the end date be in a different month than start date?", but did not saw a clear answer for this question, so I assumed based on your structure that all columns will refer to current month.

My guess is that the columns structure is confusing, how will they look with data from 2 months? You have now numbers from 1 to 15 as column names, I doubt that you can add 2 months as days in columns, how will they look like? 1...31, then another set of columns with 1..30 numbers? You cannot have duplicates in column names...

Please provide a structure that matches your description, with columns spanning 2 months.

As you see in the query provided, we keep only Name and Start/end date columns, all other columns will be un-pivoted.

Those columns should at least have the month number, not just the day, not to mention that it can be in two years as well, if one month is december and the next is january next year.

The full date is best to use as column names, just remove the step that extracts the day from Start/end dates and simply compare the Attribute column (that should have now full dates) with start/end dates, in #"Added Column" step: each [Attribute]>=[Start date] and [Attribute]<=[End date]

Attribute column should be formatted ad Date.

Avatar
Chris Yap
Member
Members
Level 0
Forum Posts: 162
Member Since:
August 21, 2019
sp_UserOfflineSmall Offline
7
March 19, 2020 - 4:49 pm
sp_Permalink sp_Print

Apologise Sir,  forgotton to insert the mock source to you,  by the way,  manage to develop the query and see whether you have more efficient way,  or that is the best already

 

Note :-  attached revised,  tab 1 source is the flat table,  tab 3 is what I developed and load to excel,   tab 2 is the desired result

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
8
March 21, 2020 - 3:35 pm
sp_Permalink sp_Print

Glad to hear you managed to make it work.

By the way, the last file source data is completely different to the initial file provided, I thought that was the initial data structure.

Next time, please try to clarify the problem from the beginning, by showing the original structure and the desired structure, this one was misleading.

Cheers,

Catalin

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Velouria, Riny van Eekelen
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
michael serna
mashal sana
Tiffany Kang
Leah Gillmore
Sopi Yuniarti
LAFONSO HERNANDEZ
Hayden Hao
Angela chen
Sean Moore
John Chisholm
Forum Stats:
Groups: 3
Forums: 24
Topics: 6216
Posts: 27250

 

Member Stats:
Guest Posters: 49
Members: 31897
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.