Тёмный

Excel - Power Query Insert Blank Row After Each Group - Episode 2552 

MrExcel.com
Подписаться 155 тыс.
Просмотров 9 тыс.
50% 1

Microsoft Excel Tutorial: Add a blank row after each group in Power Query.
Welcome to another tutorial on Power Query! In this video, we will be learning how to insert a blank row after each group in our data. This is a common task that many of us encounter while working with data, and it can be quite frustrating to have to go back to Excel to add those blank rows. But fear not, because with the help of some amazing code shared by our viewers, we will be able to do this right in the Power Query editor.
A few weeks ago, in video 2359, I was cleaning a fixed-width text file in Power Query. And towards the end, I needed to add a blank row after every group of names. I had to resort to using Excel for this task, but thanks to the suggestions from our viewers - Bill Szysz, Radoslaw Poprawski, Geert Delmulle, and Rico - we now have a code that can help us achieve this in Power Query. I also reached out to my friend Suat Ozgur, who is currently writing a book on Power Query, for some guidance on this task.
In this tutorial, we will be using a hybrid approach. We will try to do as much as we can in the Power Query editor, but for the final step of adding the blank rows, we will have to use some M code. So let's get started! Our goal is to group all the records for each person together and then add a blank row after each group. Now, this may not be the best practice, but it was a requirement for the data that I was working with. So let's not debate on whether we should have blank rows in our data or not, and focus on how we can achieve this task.
First, we will convert our data into a table by using the shortcut Ctrl + T. We will name this table "Data" and then go to the Data tab and select "From Table and Range". Next, we will group our data by the name column using the Transform > Group By option. This may seem familiar as we have used the Group By function before to total values. But this time, we will use it to get all the rows for each person in a separate table. This will create a new column called "AllRows" with a tiny table for each person.
Now comes the tricky part - adding the blank rows. We will have to write some M code for this, so let's go to the Advanced Editor. One thing that I find frustrating about the Power Query editor is the use of variable names with spaces in between. This means we have to use quotes and a hashtag to refer to these variables. So I like to fix these variable names before proceeding with the code. Once that's done, we will add a new line of code using the comma and paste in the code shared by our viewers. This will create a new step called "AddBlankRow" which we will then apply to our data.
After applying the new step, we can remove the "AllRows" column and expand the data using the original column names as prefixes. And voila! We now have our data with the blank rows inserted after each group. This may seem like a daunting task, especially for those who are new to M code, but trust me, it's not that bad. And as I've learned from my friend Suat's book, there's a lot more that we can do with M code outside of the Power Query editor. So don't be afraid to dive in and explore the possibilities.
I want to thank all of you for your support and for sharing your knowledge with me. It's always a pleasure to learn from our viewers and to share that knowledge with others. I hope this tutorial has been helpful, and I'll see you in the next one. Don't forget to subscribe to our channel for more Power Query tips and tricks. Until next time, this is Bill Jelen, signing off.
Buy Bill Jelen's latest Excel book: www.mrexcel.com/products/latest/
You can help my channel by clicking Like or commenting below: www.mrexcel.com/like-mrexcel-...
Several people commented with code to add a blank row after each group in Power Query. Thanks to ‪@BillSzysz1‬ ‪@ExcelInstructor‬ ‪@GeertDelmulle‬ Rico S and Suat Ozgur for guiding me on how to add one line of M code to my query.
Table of Contents
(0:00) Question: Insert blank row after each group in Excel
(1:14) Edit in Power Query Editor Group by Name with All Rows
(1:53) Advanced Editor - cleaning recorded M Code
(2:35) Power Query Table.InsertRows
(3:34) More cleaning in Power Query Editor
(4:47) Clicking Like really helps the algorithm
#excel #microsoft #microsoftexcel #exceltutorial #exceltips #exceltricks #excelmvp #freeclass #freecourse #freeclasses #excelclasses #microsoftmvp #walkthrough #evergreen #spreadsheetskills #analytics #analysis #dataanalysis #dataanalytics #mrexcel #spreadsheets #spreadsheet #excelhelp #accounting #tutorial
This video answers these common search terms:
insert blank rows in excel power query
Join the MrExcel Message Board discussion about this video at www.mrexcel.com/board/threads...

Опубликовано:

 

3 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 21   
@roywilson9580
@roywilson9580 Год назад
There is always a new trick to learn, I have found you learn them when you need to do something specific. Thank heaven for RU-vid creators who nurse you through the steps you need to take, can save a lot of time flicking back and forth through the documentation.
@excelisfun
@excelisfun Год назад
Thanks Mr Excel!
@chrism9037
@chrism9037 Год назад
Awesome, thanks Mr. Excel!
@johnborg5419
@johnborg5419 Год назад
Thanks Mr Excel!!!
@mattschoular8844
@mattschoular8844 Год назад
We were all apprentices at one point.... I am still at that point.... Thanks Mr.Excel
@MohdAzam-uh9jt
@MohdAzam-uh9jt Год назад
Awesome sir
@muzniahamed5887
@muzniahamed5887 Год назад
From Sri Lanka 🇱🇰🇱🇰🇱🇰
@iankr
@iankr Год назад
Many thanks, Bill. I've searched online for that Suat Ozgur book you advertise there, but I can't find it. Do you know where it's available? Been looking for a book on M that goes being the ribbon UI.
@Aahzmadius
@Aahzmadius Год назад
Can I pre-order Suat's book? Can't wait to get my hands on it!
@peltiertech1879
@peltiertech1879 4 месяца назад
Hey, pretty cool. I was looking for a Dynamic Array approach because mine was ugly. But maybe the client is fine with using Power Query.
@GeertDelmulle
@GeertDelmulle Год назад
OK, second attempt (the YT police jettisoned my first comment): Hey Mr. Excel, here's a little improvement on your query: In your GroupBy step (Table.Group) just replace "each _" in the aggregation line by: "each Table.Combine({_,#table({},{{}})})". That way you don't need to do the extra step of transforming a column to add the blank line. In PQ-M you can always amend the formulas that are generated by the interface. PS: thanks for mentioning me, really appreciate it.🙂
@josh_excel
@josh_excel Год назад
I like it. Here is a table with with two blank rows: = #table({},{{},{}})
@GeertDelmulle
@GeertDelmulle Год назад
@@josh_excel Correct! 🙂 (and you can even create a completely empty table like this, but adding that to the bottom of another table does absolutely nothing - as one might expect. :-)
@andrewdo9319
@andrewdo9319 Год назад
@@josh_excel woah that was much easier! Do you know how I could add the blank row above the first difference instead of below?
@josh_excel
@josh_excel Год назад
@@andrewdo9319 Just reverse the order: each Table.Combine({#table({},{{}}),_})
@josh_excel
@josh_excel Год назад
Can also use ampersand: each _ & #table({},{{}})
@canirmalchoudhary8173
@canirmalchoudhary8173 Год назад
This is crazy
@Excelambda
@Excelambda Год назад
PQ is king!! For fun a lambda to solve it, deals with tables or arrays (headers or no headers): ar: any array or table [h]: headers argument, omitted when array has no headers, 1 if table or array with headers =LAMBDA(ar,[h], LET( a, IF(h, DROP(ar, 1), ar), c, SEQUENCE(, COLUMNS(a)), s, SORT(a), t, TAKE(s, , 1), u, UNIQUE(t), r, REDUCE("",u, LAMBDA(v,i, LET( x, XMATCH(i, t),y, XMATCH(i, t, , -1), VSTACK(v, INDEX(s, SEQUENCE(y - x + 1, , x), c), "")))), d, DROP(DROP(IFNA(r, ""), 1), -1), IF(h, VSTACK(TAKE(ar, 1), d), d) ) )
@josh_excel
@josh_excel Год назад
Thanks! This will come in handy. How would it change if I wanted to add the word "End" after each group then a blank space?
@Excelambda
@Excelambda Год назад
@@josh_excel You're very welcome!! Here is a function for you with this versatility added. GROUPINS(ar,[ai],[h]) ar: any array or table with headers or not [ai]: array insert, any array, if omitted, a blank row will be inserted, if not, the entire array ai will be inserted [h]: headers argument, omitted when array has no headers, 1 if table or array with headers =LAMBDA(ar, [ai], [h], LET( a, IF(h, DROP(ar, 1), ar), b, IF(ISOMITTED(ai), "", ai), c, SEQUENCE(, COLUMNS(a)), s, SORT(a), t, TAKE(s, , 1), u, UNIQUE(t), r, REDUCE("", u, LAMBDA(v, i, LET(x, XMATCH(i, t), y, XMATCH(i, t, , -1), VSTACK(v, INDEX(s, SEQUENCE(y - x + 1, , x), c), b)))), d, DROP(IFNA(r, ""), 1), e, IF(AND(TAKE(d, -1) = ""), DROP(d, -1), d), IF(h, VSTACK(TAKE(ar, 1), e), e) ) ) To insert only "End" only, (not blank row) call: =GROUPINS(array,"End") - to insert "End" and a blank row call: =GROUPINS(array,{"End";""}) - to insert a row with 2 cols followed by a blank row call; and the array has headers: =GROUPINS(array,{"Group","End";"",""},1) or: =GROUPINS(array,VSTACK({"Group","End"},""),1) Hope that helps. Also, if you want to group by any column, other than first one, can be done easily, let me know. ✌😉
Далее
5 Excel Secrets You'll Be Embarrassed You Didn't Know
17:32
Guess The Drawing! ✍️✨🧐 #shortsart
00:14
Просмотров 1,1 млн
Excel's NEW Checkboxes Are Incredibly Cool! Here's why
14:24
Introducing Python in Excel
19:01
Просмотров 1,5 млн
Power Query - Avoid "Helper Queries" (+10 Cool Tricks)
18:40
Advanced Group By Tricks in Power Query
14:37
Просмотров 88 тыс.