Grouping and summing Cells based on MATCH criteria - excel

as an example I have created a small set of Data in B3:F20 with component, type and count list etc. I have assigned a Name "TypeP" for B24:B25.
My goal is to group the components based on the type and sum their count from Input B3:F20. To show the final goal, I have manually added the result in L3:N7. In L4, multiple(here 2) instances of Component DEF with same type PA are grouped and the count is summed.
I was able to achieve my goal partially as in H3:J11, where the data was grouped based on the TypeP, but still I should be able to group the similar types.
Formula I have used in H3 is
=FILTER(INDEX(B3:F20;SEQUENCE(ROWS(B3:F20));{1\2\3});(ISNUMBER(MATCH(C3:C20;TypeP;0))=TRUE))
How can I achieve the result as shown in L3:N7?

L3: =UNIQUE(H3:I11)
N3: =SUMIFS($J$3:$J$11,$H$3:$H$11,L3,$I$3:$I$11,M3)
Select N3 and fill down as far as needed.
You could also do this in Power Query
To use Power Query
Make your TypeP a Named Range (or a Table)
Select some cell in your Data Table
Data => Get&Transform => from Table/Range
When the PQ Editor opens: Home => Advanced Editor
Make note of the Table Name in Line 2
Paste the M Code below in place of what you see
Change the Table name in line 2 back to what was generated originally.
Read the comments and explore the Applied Steps to understand the algorithm
M Code
let
//Read main table
//Change table name in next line to real name of your table in the workbook
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//set data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Component", type text}, {"Type", type text}, {"Count", Int64.Type},
{"Others1", type text}, {"Others2", type any}}),
//read in the types to filter by from a "Named Range"
// Range name is `TypeP` in the workbook
typeP = Excel.CurrentWorkbook(){[Name="TypeP"]}[Content][Column1],
//Filter for the desired types
filter = Table.SelectRows(#"Changed Type", each List.Contains(typeP,[Type])),
//Group by "component and type"
//Then sum the Count column
#"Grouped Rows" = Table.Group(filter, {"Component", "Type"},
{{"Count", each List.Sum([Count]), type nullable number}})
in
#"Grouped Rows"

Related

Merging subsequent cell values only for same ids in excel

I have a requirement here in excel where I have to populate "Facility" column with Country and City names in a single cell for the distributed data in C and D columns but only for the same id.
I have attached the image for reference
Thanks for your time in advance
I tried CONCAT function and TEXTJOIN function but that didn't help
This can be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
Select some cell in your Data Table
Data => Get&Transform => from Table/Range
When the PQ Editor opens: Home => Advanced Editor
Make note of the Table Name in Line 2
Paste the M Code below in place of what you see
Change the Table name in line 2 back to what was generated originally.
Read the comments and explore the Applied Steps to understand the algorithm
M Code
let
//Change next line to reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table31"]}[Content],
//set data types for each column
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"Country", type text}, {"City", type text}}),
//Group by ID
#"Grouped Rows" = Table.Group(#"Changed Type", {"Id"}, {
//for each subgroup, group by Country
{"Facility", (t)=> let
grp=Table.Group(t,{"Country"},{
//Then combine all the cities in one text string
"Cities", (tc)=> "(" & Text.Combine(tc[City],",") & ")"}),
//Add index column to "number" the different country/cities combinations
#"Add Index" = Table.AddIndexColumn(grp, "Index",1),
#"Index to Text" = Table.TransformColumns(#"Add Index",{"Index", each Number.ToText(_,"0\. ")}),
//combine the separate subtable columns into one string
#"Combine Columns" = Table.CombineColumns(
#"Index to Text",{"Index","Country","Cities"},Combiner.CombineTextByDelimiter(""),"Facility"),
//combine the separate rows into a single row
#"Combine to One Row" = Text.Combine(#"Combine Columns"[Facility]," ")
in
#"Combine to One Row", type text},
{"All", each _, type table [Id=nullable number, Country=nullable text, City=nullable text]}}),
//Expand the Country and City columns
#"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Country", "City"})
in
#"Expanded All"

Copy value inside column based on another column (ID) in Excel or Jamovi

I can't figure out how to make it work. Some people asked similar questions in r-forums but it seems my understanding is to basic to make the transfer.
I have a large repeated measures dataset in long format and i performed a cluster-analysis. Now I need to copy the value signalling group-membership inside the second column based on Participant-ID.
The table underneath shows how it looks at the moment. I need to automatically fill in the blank spaces for cluster_membership with the same values depending on the id.
ID
Cluster_membership
1
1
3
1
2
4
2
3
5
3
ID
Cluster_membership that i need
1
3
1
3
1
3
2
4
2
4
3
5
3
5
Thank you in advance,
Philipp
If your data is as you show it, with only a single Cluster_membership per ID, you can obtain your desired output using Power Query, available in Windows Excel 2010+ and Office 365 Excel
Select some cell in your original table
Data => Get&Transform => From Table/Range
When the PQ UI opens, navigate to Home => Advanced Editor
Make note of the Table Name in Line 2 of the code.
Replace the existing code with the M-Code below
Change the table name in line 2 of the pasted code to your "real" table name
Examine any comments, and also the Applied Steps window, to better understand the algorithm and steps
M Code
let
//change table name in next line to actual name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table11"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Cluster_membership", Int64.Type}}),
//Group by ID and extract Cluster_membership
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {
{"all", each _, type table [ID=nullable number, Cluster_membership=nullable number]},
{"Cluster_membership", each List.Max([Cluster_membership])}
}),
//remove unneeded columns and expand the resultant table (except for the Cluster_membership column
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"ID"}),
#"Expanded all" = Table.ExpandTableColumn(#"Removed Columns", "all", {"ID"}, {"ID"})
in
#"Expanded all"
Note:
Another way of getting the same output would be to
Sort the table by ID and then cluster
Fill-Up the cluster column
I'm not sure which will be faster on your data set. The code with sorting is simpler, but sorting sometimes takes a while in PQ. You can try both methods. If you do, please let me know the results of comparison
M Code (sort method)
let
//change table name in next line to actual name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table11"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Cluster_membership", Int64.Type}}),
//sort by ID and cluster, then fill-up
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"Cluster_membership", Order.Ascending}}),
#"Filled Up" = Table.FillUp(#"Sorted Rows",{"Cluster_membership"})
in
#"Filled Up"

Adding a value to an existing column in Power Query Excel

How do I add a value "UTU" to the current column "List of Depot" with this logic:
If there is the value "UTU" in the List of Depot column show it in the List of Depot but if there is no "UTU" in the List of Depot then still show "UTU" with value zero.
I am using power query to connect to the data source and append query to append the multiple queries together, then I connect the pivot table in Excel to the Append query to build the report.
How do I go about this, I have tried conditional column but without success.
Current Output:
Desired Output:
You merely need to add a conditional clause at the end of your M code.
This has to be done in the Advanced Editor, and you'll need to extend the added record that I show to cover all of your columns.
M Code
let
Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Depot", type text}, {"Report 1", Int64.Type}, {"Report 2", Int64.Type}}),
//if number of columns is stable, this will work OK
//if the number of columns might vary, or be named differently, we will have to make some changes
utuRow = if List.Contains(#"Changed Type"[Depot],"UTU")
then #"Changed Type"
else Table.InsertRows(#"Changed Type",
Table.RowCount(#"Changed Type"),
{[Depot="UTU",Report 1=0,Report 2=0]})
in
utuRow

Excel Power Query > Add columns from another table

Need some guidance, please. I have two power query tables in excel, I'm looking to add columns from table B to table A where the Customer# matches. I don't want to use vlookup formulas due to performance so I was wondering if with power query this is possible.
Here is an example:
Thanks a lot!
The M-Code for merging TableA with TableB would look like that
let
Source = Excel.CurrentWorkbook(){[Name="TableA"]}[Content],
chgType = Table.TransformColumnTypes(Source,{{"Customer Number", type text}, {"Certification", type text}}),
mergeQueries = Table.NestedJoin(chgType, {"Customer Number"}, TableB, {"Customer Number"}, "TableB", JoinKind.LeftOuter),
extendTbl = Table.ExpandTableColumn(mergeQueries, "TableB", {"Crd Limit"}, {"Crd Limit"})
in
extendTbl
You need to import TableB into Powerquery beforehand, as well.
Further reading on this
Microsoft documentation
Excel Guru Blog
I think you can get what you want by to a JoinKind.Inner.
Note that this will return customer ID's that are present in both tables. If that is not the case, and you want unmatched ID's to be returned, you'll need to do a .NestedJoin with JoinKind.FullOuter and then expand the resulting table.
eg:
let
Source1 = Excel.CurrentWorkbook(){[Name="TableA"]}[Content],
tabA = Table.TransformColumnTypes(Source1,{{"Customer Number", type text}, {"Certification", type text}}),
Source2 = Excel.CurrentWorkbook(){[Name="TableB"]}[Content],
tabB = Table.TransformColumnTypes(Source2,{{"Customer Number", type text}, {"Crd Limit", Currency.Type}}),
joinTbl = Table.Join(tabA,"Customer Number",tabB,"Customer Number",JoinKind.Inner)
in
joinTbl

How can I have the informations from 2 differents columns represented as 1 in a pivot table?

In my data, I have 2 columns who represent a country visited before and a country visited after the cities that I am studying.
Here's a picture of my data sample: https://i.imgur.com/kS4K9uK.png
I'd like to represent in my pivot table all the countries linked to each city (so before and after the city). I'd like to have the cities in my line and all the countries who can possibly be visited before and after as my columns and the count of those in my values.
Here is a picture of what I'd like to achieve, but I can only do it for one of the columns (country after in that case). I'd like the same format but having the data of both before and after (but it's important to know that it's not necessarily the same countries in the 2 columns so I can't just have one of the country columns as the head and both as the values): https://i.imgur.com/PUjhSmB.png
When I place the cities in the line and the 2 country columns in value and columns, it is so difficult to read the table as the before and after are all separate and might even be counted as a pair. and if they are not in the pivot table column they only give me the count of countries before and after but not by the countries, which is not what I'm looking for.
Here is a picture of the result of the pivot table: https://i.imgur.com/3j4BD3k.png
I also tried to create a new field by doing «Country before» + «Country after» but it doesn't seem to work as the data is in text.
Ok I think understand the output now. You essentially want a count of the number of occurrences of each country in columns B+C, grouped by the city. I'll provide a few ways so you can select what suits you best.
Simplest method
The easiest way I can think of is simply paste the second column under the first column and then pivot on this new table.
COUNTIF
A more repeatable way would be to essentially make your own pivot table and use the COUNTIF function to count the instances of each country.
=COUNTIFS($A$1:$A$6,$F2,$B$1:$B$6,G$1)+COUNTIFS($A$1:$A$6,$F2,$C$1:$C$6,G$1)
Power Query
The most repeatable way is to use PowerQuery. This will enable you to refresh the data at the click of a button. To do this (assuming you have excel 2016) go to the Data tab and, with you data selected click "From Table/Range". The Power Query window will open. On the top left of the screen will be a button with advanced editor. When you open it you'll see the following code:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Country Before", type text}, {"Country After", type text}})
in
#"Changed Type"
Replace the code with the following code. Note that your table may be called something different. You can see what it's called on the second line of the code. The code below uses "Table1" - you can replace this with the name of your table.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Country Before", type text}, {"Country After", type text}}),
#"Before" = Table.RemoveColumns(#"Changed Type",{"Country Before"}),
#"After" = Table.RemoveColumns(#"Changed Type",{"Country After"}),
#"Append" = Table.Combine({#"Before",#"After"}),
#"Inserted Merged Column" = Table.AddColumn(Append, "Country", each Text.Combine({[Country After], [Country Before]}, ""), type text),
#"Removed Columns" = Table.RemoveColumns(#"Inserted Merged Column",{"Country After", "Country Before"})
in
#"Removed Columns"
Hope that helps.

Resources