Forum Discussion
Adding a new column with value from a specific cell. Then append all sheets from a single excel
- 4 years ago
Hi NS_powerbi ,
Paste the following code over the default code in Advanced Editor of a new blank query to see the steps I took:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY9ND4IwDIb/CuGkCcnGYAOOIqIeCIncJBymNtHIhyFo4N/bApGDy9L3bfa0a/PcTPWzhMFYCcnXpmUut7ByM3u3ta4A07hpYba76mUcIzRRGmLcQ32DdsI7+BCxKaFHsW0pFSoPGFfMDnyBSTKSiSbgrK93FE+50iXOZVwi5/Efd2ou6MMxKjz0wgXjDrVbsAx015VgrFwu/rcINZUfmgoGVN+X0282w07zVPEIbkvdUk/dP2raQ0ohHEI9hjQOpia0+AI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]), cleanBlanks = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Column2", "Column3", "Column4", "Column5"}), addShopToSplit = Table.AddColumn(cleanBlanks, "Shop", each if [Column1] <> null and [Column3] = null then [Column1] else null), splitToShopShopId = Table.SplitColumn(addShopToSplit, "Shop", Splitter.SplitTextByEachDelimiter({" ("}, QuoteStyle.Csv, false), {"Shop.1", "Shop.2"}), repCloseBracket = Table.ReplaceValue(splitToShopShopId,")","",Replacer.ReplaceText,{"Shop.2"}), fillDownShopShopId = Table.FillDown(repCloseBracket,{"Shop.1", "Shop.2"}), filterNullEmpId = Table.SelectRows(fillDownShopShopId, each ([Column3] <> null)), repR1ShopHeader = Table.ReplaceValue(filterNullEmpId, each [Shop.1], each if [Column1] = "Surname" then "Shop" else [Shop.1],Replacer.ReplaceText,{"Shop.1"}), repR1ShopIdHeader = Table.ReplaceValue(repR1ShopHeader, each [Shop.2], each if [Column1] = "Surname" then "Shop Id" else [Shop.2],Replacer.ReplaceText,{"Shop.2"}), promHeads = Table.PromoteHeaders(repR1ShopIdHeader, [PromoteAllScalars=true]), reorderCols = Table.ReorderColumns(promHeads,{"Shop", "Shop Id", "Surname", "Forename", "Emp ID", "DOB", "Gender"}) in reorderColsYou probably won't need the 'cleanBlanks' step as I understand your actual data contains pure nulls.
This gives me the following output:
As I mentioned before, this is completely bespoke to the exact situation that you have presented and, therefore, you will need to understand the principles and functions used in order to amend it to a new scenario if required.
Pete
Here is what I get after cleaning the data in power query editor
| Oakley (250) | Null | Null | Null | Null |
| Surname | Forename | Emp ID | DOB | Gender |
| Steve | Alex | 11556 | 09/06/1982 | M |
| Max | Zach | 76454 | 04/05/1970 | M |
| Rob | Bob | 66660 | 02/03/1980 | M |
| Seattle (402) | Null | Null | Null | Null |
| Bab | Homey | 88554 | 01/02/1982 | F |
| Clar | Maxine | 55223 | 07/01/1976 | F |
There are 40 shops. And the number of employees are different in each sheet. Now I want to use the row with the surname as header, amd create two new columns to enter the shop name and the shop id. To get result like this.
| Shop | Shop id | Surname | Forename | Emp ID | DOB | Gender |
| Oakley | 250 | Steve | Alex | 11556 | 09/06/1982 | M |
| Oakley | 250 | Max | Zach | 76454 | 04/05/1970 | M |
| Oakley | 250 | Rob | Bob | 66660 | 02/03/1980 | M |
| Seattle | 402 | Bab | Homey | 88554 | 01/02/1982 | F |
| Seattle | 402 | Clar | Maxine | 55223 | 07/01/1976 | F |
Hope this makes my problem clearer. Thanks a lot. Really appreciate it.
- BA_Pete4 years agoSuper User
Hi NS_powerbi ,
Paste the following code over the default code in Advanced Editor of a new blank query to see the steps I took:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY9ND4IwDIb/CuGkCcnGYAOOIqIeCIncJBymNtHIhyFo4N/bApGDy9L3bfa0a/PcTPWzhMFYCcnXpmUut7ByM3u3ta4A07hpYba76mUcIzRRGmLcQ32DdsI7+BCxKaFHsW0pFSoPGFfMDnyBSTKSiSbgrK93FE+50iXOZVwi5/Efd2ou6MMxKjz0wgXjDrVbsAx015VgrFwu/rcINZUfmgoGVN+X0282w07zVPEIbkvdUk/dP2raQ0ohHEI9hjQOpia0+AI=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t]), cleanBlanks = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Column2", "Column3", "Column4", "Column5"}), addShopToSplit = Table.AddColumn(cleanBlanks, "Shop", each if [Column1] <> null and [Column3] = null then [Column1] else null), splitToShopShopId = Table.SplitColumn(addShopToSplit, "Shop", Splitter.SplitTextByEachDelimiter({" ("}, QuoteStyle.Csv, false), {"Shop.1", "Shop.2"}), repCloseBracket = Table.ReplaceValue(splitToShopShopId,")","",Replacer.ReplaceText,{"Shop.2"}), fillDownShopShopId = Table.FillDown(repCloseBracket,{"Shop.1", "Shop.2"}), filterNullEmpId = Table.SelectRows(fillDownShopShopId, each ([Column3] <> null)), repR1ShopHeader = Table.ReplaceValue(filterNullEmpId, each [Shop.1], each if [Column1] = "Surname" then "Shop" else [Shop.1],Replacer.ReplaceText,{"Shop.1"}), repR1ShopIdHeader = Table.ReplaceValue(repR1ShopHeader, each [Shop.2], each if [Column1] = "Surname" then "Shop Id" else [Shop.2],Replacer.ReplaceText,{"Shop.2"}), promHeads = Table.PromoteHeaders(repR1ShopIdHeader, [PromoteAllScalars=true]), reorderCols = Table.ReorderColumns(promHeads,{"Shop", "Shop Id", "Surname", "Forename", "Emp ID", "DOB", "Gender"}) in reorderColsYou probably won't need the 'cleanBlanks' step as I understand your actual data contains pure nulls.
This gives me the following output:
As I mentioned before, this is completely bespoke to the exact situation that you have presented and, therefore, you will need to understand the principles and functions used in order to amend it to a new scenario if required.
Pete
- NS_powerbi4 years agoNew Member
Thanks a lot Pete. I was able to implement it on my dataset perfectly.