• 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
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Power Pivot formula in Power query|Power Query|Excel Forum|My Online Training Hub

You are here: Home / Power Pivot formula in Power query|Power Query|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
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 QueryPower Pivot formula in Power query
sp_PrintTopic sp_TopicIcon
Power Pivot formula in Power query
Avatar
John Jack
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 27, 2023
sp_UserOfflineSmall Offline
1
April 10, 2023 - 8:30 am
sp_Permalink sp_Print sp_EditHistory

Hi all, 

I need to calculate the number of weeks included in a month for recurring dates. I have managed to achieve this result with a combination of power query and power pivot but I have realized now that I need to achieve this only with power query (I need to group by AFTER performing some calculations with the number of weeks). 

The formulas used in power pivot are:

=COUNTROWS(FILTER(Table1,Table1[Start of Month]=EARLIER(Table1[Start of Month])))

and

=CALCULATE(COUNT(Table1[Date]),ALLEXCEPT(Table1,Table1[Date]))

The above formulas are both included in the "Helper1" and "Helper2" column in Power Pivot and I used them to calculate the number of weeks in the referring month. 

Is there anyone able to achieve this in power query without using group by? I will be grateful forever 😀

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 689
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
April 11, 2023 - 9:45 pm
sp_Permalink sp_Print

You could do something like this:

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Duplicated Column" = Table.DuplicateColumn(Source, "Weeks", "Weeks - Copy"),
WeeksToDates = Table.ReplaceValue(#"Duplicated Column",each [Weeks],each Date.AddDays(Date.EndOfWeek(DateTime.LocalNow(),Day.Friday),(Number.From(Text.Replace([Weeks], "plans_week", "")) -1) * 7),Replacer.ReplaceValue,{"Weeks"}),
#"Renamed Columns" = Table.RenameColumns(WeeksToDates,{{"Weeks", "Date"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
DateList = #"Changed Type"[Date],
#"Renamed Columns1" = Table.RenameColumns(#"Changed Type",{{"Weeks - Copy", "Weeks"}}),
#"Inserted Start of Month" = Table.AddColumn(#"Renamed Columns1", "Start of Month", each Date.StartOfMonth([Date]), type date),
SOMs = #"Inserted Start of Month"[Start of Month],
#"Reordered Columns" = Table.ReorderColumns(#"Inserted Start of Month",{"Weeks", "Date", "Start of Month"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "Custom", each let som = [Start of Month], d = [Date] in List.Count(List.Select(SOMs, each _ = som)) / List.Count(List.Select(DateList, each _ = d)), Int64.Type)
in
#"Added Custom"

Avatar
John Jack
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 27, 2023
sp_UserOfflineSmall Offline
3
April 13, 2023 - 2:27 pm
sp_Permalink sp_Print

Hi Velouria, 

Thank you very much for your help. Unfortunately I won't be able to use it because my original data set has hundreds of thousands of rows and that formula kills the workbook.

However I really liked the way you replaced the week-ending dates. Would you please be able to advise how would you change that formula with unpivoted columns (i.e. plans_week* in each column)? See it attached please.

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 689
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
April 17, 2023 - 6:27 pm
sp_Permalink sp_Print

Sure - you can do something like this:

 

let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
RenamedColumns = Table.TransformColumnNames(Source,
(cn as text) as text =>
if Text.Start(cn, 10) = "plans_week" then Date.ToText(Date.AddDays(Date.EndOfWeek(DateTime.Date(DateTime.LocalNow()),Day.Friday), (Number.From(Text.Replace(cn, "plans_week", "")) -1) * 7), "dd/MM/yyyy") else cn)
in
RenamedColumns

Avatar
John Jack
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 27, 2023
sp_UserOfflineSmall Offline
5
April 19, 2023 - 11:33 am
sp_Permalink sp_Print

Brilliant! Thanks!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Daisy Tang
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
Hans Hallebeek: 185
Newest Members:
Kwaje Alfred Mogga
thong nguyen
Appiagyei Kofi Frimpong
Hilary Burchfield
Richie Wright
Adel Kock
Barbara Murray
Doug Milne
Siobhan Stringer
Rob Rooth
Forum Stats:
Groups: 3
Forums: 24
Topics: 6544
Posts: 28650

 

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

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • 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

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

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

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.