Excel: how to count combinations of cells over multiple columns? - excel

Example of my data
If I have the data as shown in the picture (real data has same form but is much larger), how would I count how many times a certain combination, for example the combination Dinner - Pasta, occurs per ID? Ideally I would like to make a table in another tab showing per ID the count for all possible combinations.
Thanks in advance!

Try SUMPRODUCT:
=SUMPRODUCT((I2=$A$2:$A$7)*(J2=$B$2:$F$7)*(K2=$C$2:$G$7))

Highlight your entire and Insert - Table
In the table ribbon, change your table name to "InputTable"
In the Get & Transform section of the Data ribbon, click From Table. This will bring up a PowerQuery window. In the PowerQuery window:
Create a new query (Click either Home - Manage - Reference... or Home - New Sources - Other Sources - Blank Query... it doesn't really matter, we just want to create a new query and we're going to replace its contents in the next steps anyway)
Change the name in the (right sidebar) to "ffTableForDay"
Click Home - Advanced Editor
Insert the following code:
// Called "ffTable*" because it's a Function that returns a Function that returns a Table.
// Returns a function specific to a table that takes a day.
// Returned function takes a day and returns a table of meals for that day.
(table as table) as function => (day as text) as table =>
let
#"Type Column Name" = day & "_type",
#"Food Column Name" = day & "_Food",
#"Removed Other Columns" = Table.SelectColumns(table,{"ID", #"Type Column Name", #"Food Column Name"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Other Columns",{{#"Type Column Name", "Type"}, {#"Food Column Name", "Food"}}),
#"Removed Blank Rows" = Table.SelectRows(#"Renamed Columns", each [Type] <> null and [Type] <> "" and [Food] <> null and [Food] <> ""),
#"Add Day" = Table.AddColumn(#"Removed Blank Rows", "Day", each day, type text)
in
#"Add Day"
Create a new query
Change the query name to "Meals"
Click Home - Advanced Editor
Insert the following code:
let
Source = InputTable,
Days = {"Monday", "Tuesday", "Wednesday"},
#"Function Per Day" = ffTableForDay(Source),
// get list of tables per call to ffTableForDay(InputTable)(day)
#"Table Per Day" = List.Transform(Days, #"Function Per Day"),
Result = Table.Combine(#"Table Per Day")
in
Result
Create a new query
Change the query name to "ComboCount"
Click Home - Advanced Editor
Insert the following code:
let
Source = Meals,
// Created by clicking **Transform - Group By** and then, in the dialog box, clicking advanced and grouping by Food and Type
#"Grouped Rows" = Table.Group(Source, {"Type", "Food"}, {{"Count", each Table.RowCount(_), type number}})
in
#"Grouped Rows"
Click Home - Close & Load
If your query options were set to load queries to the workbook (default), then delete the "Meals" tab, if you wish. If your query options were to NOT load queries to the workbook by default then right-click on the "ComboCount" query in the side-bar and click "Load To..."
Alternatively
Once we have the "Meals" query working, instead of creating a "ComboCount" query, we could have
loaded Meals to the workbook and done a pivot table, or
loaded Meals to the data model and done a Power Pivot.

Related

How to combine multiple columns from a table

My issue is the following: I have a table where I have multiple columns that have date and values but represent different things. Here is an example for my headers:
I Customer name I Type of Service I Payment 1 date I Payment 1 amount I Payment 2 date I Payment 2 amount I Payment 3 date I Payment 3 amount I Payment 4 date I Payment 4 amount I
What I want to do is sumifs the table based on multiple criteria. For example:
I Type of Service I Month 1 I Month 2 I Month 3 I Month 4
Service 1
Service 2
Service 3
The thing is that I do not want to write 4 sumifs (in this case, but in fact I have more that 4 sets of date:value columns).
I was thinking of creating a new table where I could put all the columns below each other (in one table with 4 columns - Customer name, Type of Service, Date and Payment) but the table should be dynamically created, meaning that it should be expanded dynamically with the new entries in the original table (i.e. if the original table has 200 entries, this would make the new table with 4x200=800 entries, if the original table has one more record then the new table should have 4x201=804 records).
I also checked the PowerQuery option but could not get my head around it.
So any help on the matter will be highly appreciated.
Thank you.
You can certainly create your four column table using Power Query. However, I suspect you may be able to also generate your final report using PQ, so you could add that to this code, if you wish.
And it will update but would require a "Refresh" to do the updating.
The "Refresh" could be triggered by
User selecting the Data/Refresh option
A button on the worksheet which user would have to press.
A VBA event-triggered macro
In any event, in order to make the query adaptable to different numbers of columns requires more M-Code than can be generated from the UI, a well as a custom function.
The algorithm below depends on the data being in this format:
Columns 1 and 2 would be Customer | Type of Service
Remaining columns would alternate between Date | Amount and be Labelled: Payment N Date | Payment N Amount where N is some number
If the real data is not in that format, some changes to the code may be necessary.
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
To enter the Custom Function, while in the PQ Editord
Right click in the Queries Pane
Add New Query from Blank Query
Paste the custom function code into the Advanced Editor
rename the Query fnPivotAll
M Code
let
//Change Table name in next line to be the Actual table name in your workbook
Source = Excel.CurrentWorkbook(){[Name="Table8"]}[Content],
/*set datatypes dynamically with
first two columns as Text
and subsequent columns alternating as Date and Currency*/
textType = List.Transform(List.FirstN(Table.ColumnNames(Source),2), each {_,Text.Type}),
otherType = List.RemoveFirstN(Table.ColumnNames(Source),2),
dateType = List.Transform(
List.Alternate(otherType,1,1,1), each {_, Date.Type}),
currType = List.Transform(
List.Alternate(otherType,1,1,0), each {_, Currency.Type}),
colTypes = List.Combine({textType, dateType, currType}),
typeIt = Table.TransformColumnTypes(Source,colTypes),
//Unpivot all except first two columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(typeIt, List.FirstN(Table.ColumnNames(Source),2), "Attribute", "Value"),
//Remove "Payment n " from attribute column
remPmtN = Table.TransformColumns(#"Unpivoted Other Columns",{{"Attribute", each Text.Split(_," "){2}, Text.Type}}),
//Pivot on the Attribute column without aggregation using Custom Function
pivotAll = fnPivotAll(remPmtN,"Attribute","Value"),
typeIt2 = Table.TransformColumnTypes(pivotAll,{{"date", Date.Type},{"amount", Currency.Type}})
in
typeIt2
Custom Function: fnPivotAll
//credit: Cam Wallace https://www.dingbatdata.com/2018/03/08/non-aggregate-pivot-with-multiple-rows-in-powerquery/
(Source as table,
ColToPivot as text,
ColForValues as text)=>
let
PivotColNames = List.Buffer(List.Distinct(Table.Column(Source,ColToPivot))),
#"Pivoted Column" = Table.Pivot(Source, PivotColNames, ColToPivot, ColForValues, each _),
TableFromRecordOfLists = (rec as record, fieldnames as list) =>
let
PartialRecord = Record.SelectFields(rec,fieldnames),
RecordToList = Record.ToList(PartialRecord),
Table = Table.FromColumns(RecordToList,fieldnames)
in
Table,
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Values", each TableFromRecordOfLists(_,PivotColNames)),
#"Removed Other Columns" = Table.RemoveColumns(#"Added Custom",PivotColNames),
#"Expanded Values" = Table.ExpandTableColumn(#"Removed Other Columns", "Values", PivotColNames)
in
#"Expanded Values"
Sample Data
Output
If this does not give you what you require, or if you have issues going further with it to generate your desired reports, post back.

Power Query / Power BI get look for data from another excel workbook

I am trying to combine worksheets from two different workbooks with Power Query and I have trouble doing that.
I do not want to merge the two workbooks.
I do not want to create relationships or "joints".
However, I want to get very specific information for one workbook which has only one column. The "ID" column.
The ID column has rows with letter tags : AB or BE.
Following these letters, sepcific numeric ranges are associated.
For both AB and BE, number ranges first from 0000 to 3000 and from 3000 to 6000.
I thus have the following possibilities:
From AB0000 to AB3000
From AB3001 to AB6000
From BE0000 to BE3000
From BE3001 to AB6000
Each category match to the a specific item in my column geography, from the other workbook:
From AB0000 to AB3000, it is ItalyZ
From AB3001 to AB6000, it is ItalyB
From BE0000 to BE3000, it is UKY
From BE3001 to AB6000, it is UKM
I am thus trying to find the highest number associated to the first AB category, the second AB category, the first BE category, and the second.
I then want to "bring" this number in the other query and increment it each time that matching country is found in the other workbook.
For example :
AB356 is the highest number in the first workbook.
Once the first "ItalyB" is found, the column besides writes "AB357".
Once the second is "ItalyB" is found, the column besides write "AB358".
Here is the one columned worksheet:
Here is the other worksheet with the various countries in geography:
Here is an example of results:
have one column (geography) with
I think that this is something which I should work towards:
I added the index column, with a start as one, because each row (even row zero) should increment either of the four matching code.
In order to keep moving forward I have also been trying to create some sort of mapping in third excel sheet, that I imported in Power BI, but I am not sure that this is a good way forward:
I have the following result when I create a blank query:
After a correction, I still get this result when creating the blank query:
This is not an easy answer as there are many steps to get to your result. I have choosen for m-query because of the complexity.
In PBi click on Transform data, now you are in m-query.
The table with the ID's (I called it "HighestID") needs expansion
because we need to be able to map on prefix
You need a mapping table
("GeoMapping"), else there is no relation between the Prefixes and
the geolocation.
We need the newID on the Geo-table (which I called "Geo").
Expand the HighestID table.
Click on the table and open the Advanced Editor, look at your code and compare it to the one below, the last 2 steps are essential, there I add two columns (Prefix and Number) which we need later.
let
Source = Csv.Document(File.Contents("...\HighestID.csv"),[Delimiter=";", Columns=1, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "Prefix", each Text.Middle([ID],0,2), type text),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Number", each Number.FromText(Text.Middle([ID],2,5)))
in
#"Added Custom1"
Result:
Create mapping table
Click right button under your last table and click Blank Query:
Paste the source below, ensure the name of the merg table equals the name of your table. As I mentioned, I called it HighestID.
let
Source = #table({"Prefix", "Seq_Start", "Seq_End","GeoLocation"},{{"AB",0,2999,"ItalyZ"},{"AB",3000,6000,"ItalyB"},{"BC",0,299,"UKY"},{"BC",3000,6000,"UKM"}}),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Seq_Start", Int64.Type}, {"Seq_End", Int64.Type}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Prefix"}, HighestID, {"Prefix"}, "HighestID", JoinKind.LeftOuter),
#"Expanded HighestID" = Table.ExpandTableColumn(#"Merged Queries", "HighestID", {"Number"}, {"Number"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded HighestID", each [Number] >= [Seq_Start] and [Number] <= [Seq_End]),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Prefix", "Seq_Start", "Seq_End", "GeoLocation"}, {{"NextSeq", each List.Max([Number]) + 1, type number}})
in
#"Grouped Rows"
Result:
Adding the NextSeq Column
This is the hard bit because when I would only give you teh code, I am afraid it will not work so I give you the steps you need to do.
1.Select the table, right click on Geography and click Group by. select as below:
Merge with table Geomapping as below:
Expand the GeoMapping with NextSeq
Add a custom column:
Remove columns not needed so only custom is left created in step 4.
Expand the column (all select). End result all your columns you had earlier plus an Index column.

Excel Power Query: get data from multiple unfixed files with multiple unfixed sheets

There are unfixed number of excel files in folder as per image1.
( Path may change, looking for solution as dynamic path from any cell)
There are unfixed number of sheets ( Max 10 sheets) in each file.
Each sheet have approx 10 to 40 top rows as transaction data.
After transaction data there are two summaries- Qty and Amount ( unfixed starting row) 3a,3b,3c
I am looking for final output as image 4a, 4b. using power query.
Folder path of excel file; it may change.
Sample data sheets on file
Final output needed ( 2 separate sheets with two tables)
All pix in one
Sorry for the length of this response, but there are a lot of steps involved and I included quite a bit of screen clips as well. I believe this solution does what you are looking for.
I start with files in a folder:
Feb-19.xlsx contains two tabs:
Jan-19.xlsx contains three tabs:
I open a new Excel file, then click Data > New Query > From File > From Folder and either type in, or use the Browse button to navigate to, the location of the folder that has the files. (When I navigate to my OneDrive folder, my path has SkyDrive.old in it, but it is my OneDrive folder that you saw in the first image above.) Then I click OK:
Then I click Transform Data:
This appears:
I click Home > Manage Parameters (the words, with the drop-down arrow) > New Parameter, and I set it up like this and click OK.
After clicking OK, this appears:
You can see that I entered the path for the folder that has the files. I can change this parameter value later if I want to use a different folder path.
To do that, I would click on in the left pane. Clicking it would bring me to this same place, where I can edit the value.
Now, I click on the query that I had already started. It is currently the only other item in the left pane. Clicking it brings this back up on the screen:
I edit the text in the formula bar, replacing "C:\Users\MARC_000\SkyDrive.old\Test" with FolderPath. The result is the exact same table, but the formula bar has Folder.Files(FolderPath). Now, instead of using a hard-coded reference, the query is using the parameter value.
Then, just because I want to, I change the query's name to "Main Query." You can do that by clicking on the query in the left pane, then changing the name in the PROPERTIES at the top of the right pane.
Next, I select both the Content and Name columns, and then Home > Remove Columns (the words, with the drop-down arrow) > Remove Other Columns to get this:
Then I click the button to combine the files in the Content column, which brings up this pop-up. Then I click on just the folder only, and then OK.
Now there are more query entries in the left pane:
I click on the new query, Transform Sample File from Test, and see this:
I select the Data and Item columns, and then Home > Remove Columns (the words, with the drop-down arrow) > Remove Other Columns to get this:
---SEE EDIT AT BOTTOM OF ANSWER, WHICH REPLACES THE FOLLOWING---
Then I click the button to expand the tables in the Data column, which brings up this pop-up. Then I clear the checkbox beside "Use original column name as prefix" and click OK.
---RETURN FROM EDIT AT BOTTOM OF ANSWER TO CONTINUE---
Which yields this:
Then I filter out null values from the Column1 column. (Click the down arrow at the top of the column and deselect null.)
Then I click Add Column > Conditional Column, and set it up like this, and click OK:
Which yields this:
Then I select the new Custom column and click Transform > Fill > Down, to get this:
Then I filter out "Summary (Amount)" and "Summary (Units)" entries from the Column1 column. (Click the down arrow at the top of the column and deselect 'Summary (Amount)' and 'Summary (Units)'.) Which yields this:
Now I go back to the Main Query. In other words, click on Main Query in the left pane. There will be a "problem." All I need to do is delete the last APPLIED STEP in the right pane: Changed Type. Once I delete that, all is good and I see this:
But I also want file names, so I click on the APPLIED STEP that is before the one that is currently selected, "Expanded Transform File from Test" is selected, so I click "Removed Other Columns 1", and in the formula bar, I change the code from Table.SelectColumns(#"Filtered Hidden Files1", {"Transform File from Test"}) to Table.SelectColumns(#"Filtered Hidden Files1", {"Transform File from Test", "Name"}). This adds the Name column and I see this:
Then I go back to the last APPLIED STEP, which is "Expanded Transform File from Test" and now I see this:
Then I click Transform > Use First Row as Headers and get this:
Then I rename the DE column to Sheet and the Feb-19.xlsx column to File Name.
Then I filter out "Custname" entries from the Custname column. (Click the down arrow at the top of the column and deselect 'Custname'.) Which yields this:
Then I reordered the columns to get this:
Then I select the Summary Type column and click Transform > Group By, and fill out the pop-up box like this and click OK:
Which yields this (these are your two tables):
So then I right-click on the Main Query in the left pane, and select Reference. That gives me a new query named Main Query (2), with a table that looks just like the last image above. Now I click on the table in Summary (Units) row and get this:
Then I repeat the process for the Summary (Amount): I right-click the Main Query in the left pane, select Reference, and then click on the table in the new query's Summary (Amount) Row to get this:
Lastly, I rename the two newest queries "Summary (Units)" and "Summary (Amount)"
When you close and load, this will give you three new worksheets. One for each query. If you don't want a worksheet for the Main Query (If you only want the Summary (Units) and Summary (Amount)) then, after you close and load and are back in Excel, click Data > Show Queries. Then right-click the Main Query in the right pane and click Load To, then select "Only Create Connection" and click Load. Click Continue when you get the data loss warning.
One more last thing: Do not put the Excel Workbook that has this query in it in its source folder, with the files it is getting the information from. Keep it separate.
---Edits to accommodate top rows having transactional information---
I'm adding the following to deal with sheets that might have rows of information above the Summary tables. Here's what I came up with:
In the answer above, beginning immediately after the step where I said: I select the Data and Item columns, and then Home > Remove Columns (the words, with the drop-down arrow) > Remove Other Columns to get this:
I now add another column (Add Column > Custom Column), and I set it up like this:
This makes a duplicate of the Data column, but adds an index within each of the nested tables, like this:
Then I add another column to determine the index number associated with the start of each summary in each nested table:
(You may want to search for "Summary (" or "Summary (Units)" instead of "Summary")
Note that it is constructed similarly to the previous column, in that it is basically a duplicate of the Indexed column, only with the Summary Index column added within each nested table.
Then I add another column like this, to determine the index position of the first Summary table's first line for each nested table:
and get this:
Then I add one more column like this, to remove the top rows that I don't want within each nested table:
Which gives me this:
(The table selected in this image is the one that had the extra information at the top. That information is gone now.)
Then I select the TopRowsRemoved and Item columns, and then Home > Remove Columns (the words, with the drop-down arrow) > Remove Other Columns to get this:
Then, I click the button to expand the tables in the TopRowsRemoved column (instead of the Data column, which we had done before), which brings up this pop-up that looks exactly the same as when we'd used the Data column. Then I clear the checkbox beside "Use original column name as prefix" and click OK.
Then I delete the old Expanded Data step, under APPLIED STEPS in the right hand pane. If I don't delete the Expanded Data step, I'll get an error because it's looking for the Data column, which doesn't exist. I didn't use the Data column this time. Instead, I used the TopRowsRemoved column.
At this point, the rest of my previous answer still applies, so refer back to where I wrote ---RETURN FROM EDIT AT BOTTOM OF ANSWER TO CONTINUE--- above.
Here's my M code for the "Main Query" query:
let
Source = Folder.Files(FolderPath),
#"Removed Other Columns" = Table.SelectColumns(Source,{"Content", "Name"}),
#"Invoke Custom Function1" = Table.AddColumn(#"Removed Other Columns", "Transform File from Test", each #"Transform File from Test"([Content])),
#"Filtered Hidden Files1" = Table.SelectRows(#"Invoke Custom Function1", each [Attributes]?[Hidden]? <> true),
#"Removed Other Columns1" = Table.SelectColumns(#"Filtered Hidden Files1", {"Transform File from Test", "Name"}),
#"Expanded Transform File from Test" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File from Test", {"Column1", "Column2", "Column3", "Column4", "Column5", "Item", "Custom"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Item", "Custom"}),
#"Promoted Headers" = Table.PromoteHeaders(#"Expanded Transform File from Test", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"CustName", type text}, {"Opening", type any}, {"In", type any}, {"Out", type any}, {"Closing", type any}, {"DE", type text}, {"Summary (Units)", type text}, {"Feb-19.xlsx", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"DE", "Sheet"}, {"Feb-19.xlsx", "File Name"}, {"Summary (Units)", "Summary Type"}}),
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each ([CustName] <> "CustName")),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"File Name", "Sheet", "CustName", "Opening", "In", "Out", "Closing", "Summary Type"}),
#"Grouped Rows" = Table.Group(#"Reordered Columns", {"Summary Type"}, {{"AllData", each _, type table}})
in
#"Grouped Rows"
Here's my M code for the "Transform Sample File from Test" query, with the changes to accommodate top rows having transactional information:
let
Source = Excel.Workbook(#"Sample File Parameter1", null, true),
#"Removed Other Columns" = Table.SelectColumns(Source,{"Data","Item"}),
#"Added Index" = Table.AddColumn(#"Removed Other Columns", "Indexed", each Table.AddIndexColumn([Data],"Index", 0, 1)),
#"Added Custom1" = Table.AddColumn(#"Added Index", "SummaryIndexed", each Table.AddColumn([Indexed],"Summary Index", each try if Text.StartsWith([Column1],"Summary") then [Index] else null otherwise null)),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "IndexMins", each List.Min([SummaryIndexed][Summary Index])),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "TopRowsRemoved", each Table.RemoveFirstN([SummaryIndexed],[IndexMins])),
#"Removed Other Columns1" = Table.SelectColumns(#"Added Custom3",{"TopRowsRemoved", "Item"}),
#"Expanded TopRowsRemoved" = Table.ExpandTableColumn(#"Removed Other Columns1", "TopRowsRemoved", {"Column1", "Column2", "Column3", "Column4", "Column5", "Index", "Summary Index"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Index", "Summary Index"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded TopRowsRemoved", each ([Column1] <> null)),
#"Added Custom" = Table.AddColumn(#"Filtered Rows", "Custom", each if Text.StartsWith([Column1],"Summary") then [Column1] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
#"Filtered Rows1" = Table.SelectRows(#"Filled Down", each ([Column1] <> "Summary (Amount)" and [Column1] <> "Summary (Units)"))
in
#"Filtered Rows1"

excel formula with multiple criteria (match and index?)

I have a table with following structure and it shows calendar entries:
| Title | Description | StartTime | EndTime | User |
.
I want to create a new table with the following structure and this table would show all users and their plans for the date which has given in the first row.:
| User | Date1 | Date2 | Date3 | …
.
My problem is something like this:
I want to show in the second table the titles of the rows if the Date1(or Date2 ..) is between Start- and End date. So I need an excel formula which I can write in all cells.
.
I could write a SQL statement like that (I know its syntax is not correct but I want to show what I need):
SELECT Title
FROM Table1, Table2
WHERE Date1 > StartDate AND Date1 < EndDate and User.Table1 = User.Table2
.............
Can you please help me?
Can't think of a simple way to do this.
First of all, how do you plan to display it if there are two titles that fall under the same date segment for the same user?
To me this looks like an effort to reverse engineer a summary table to a more detailed table, in which you will need to type in the individual column by dates - fill in all the missing data, then a simple pivot would do the job.
First you will need to keep only ONE date field, then populate all the dates in between start and end date.
From this:
*listing two titles - a and b for user ak to illustrate the problem where one user has multiple titles appearing within the same date segment.
To this: - populating all the dates where the title will appear
Then just pivot the new range to get this:
Instead of the title being listed out, we can see which date did it occur. Easily copy and paste the pivot as values, then replace the title count "1" with title name "a" to get below:
Assuming you would want the title concatenated by user, just copy the blue part, and get the end result below:
Do you have Power Query? if you have Excel 2016 version you have it (Get & Transform) in previous versions you can download it. it is a free add-in.
Go to Data
Select From Table/Range
ok
It will appear the Query Editor, there you can:
Change data type to "Date"
Go to Add Column
And 7. In date options select "Subtract Days"
Fix the negatives results Duration.Days([End] - [Start])
Add a "custom column" List.Dates([Start],[Subtraction]+1,#duration(1,0,0,0))
Click in the corner (doble arrow) and chose "Expand to New Rows"
Select and delete Columns that you won't need
Go to Transform
Click "Pivot Column"
In "Advanced Options" select "Don't aggregate"
ok
Go Home select "close & load"
Finally you get a new sheet with the new information.
You can add some filters to see a specific period of time...
The amazing thing about this is you can append all the data that you want, and then it will be a simple right click and refresh in the green table, and you will have your data fixed it.
This is the query if you just want to copy and paste in the "Advanced Editor"
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Title", type text}, {"Start", type date}, {"End", type date}, {"User", type text}}),
#"Inserted Date Subtraction" = Table.AddColumn(#"Changed Type", "Subtraction", each Duration.Days([End] - [Start])),
#"Added Custom" = Table.AddColumn(#"Inserted Date Subtraction", "Days", each List.Dates([Start],[Subtraction]+1,#duration(1,0,0,0))),
#"Expanded Days" = Table.ExpandListColumn(#"Added Custom", "Days"),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Days",{"Start", "End", "Subtraction"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Days", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Days", type text}}, "en-US")[Days]), "Days", "Title")
in
#"Pivoted Column"

Enriching Excel data/rows by referencing %shares in another table

In Excel, what VBA code will help me explode/enrich data in table A by applying the % shares in table B to produce the desired output in table C? Not all companies need to be enriched.
screenshot of relevant tables in Excel
I envisage some loop to match on company name and then to enrich Table B by inserting the necessary rows to show the resulting shared $ by Team.
I'd use Power Query for that, not VBA. Load both table A and Table B into Power Query. Then create a query that merges the two tables on the company column, using a full outer join. Then extract the team and share columns. Create a new column for the calculation of the $ value, delete the columns not required and remove null values from the Team column. The result looks like this:
M Code generated by clicking the buttons in Power Query Editor and entering one IF statement manually looks like this:
let
Source = Table.NestedJoin(TableA,{"company"},TableB,{"company"},"NewColumn",JoinKind.FullOuter),
#"Expanded NewColumn" = Table.ExpandTableColumn(Source, "NewColumn", {"Team", "PercShare"}, {"Team", "PercShare"}),
#"Added Custom" = Table.AddColumn(#"Expanded NewColumn", "Dollars", each if [Team] = null then [dollar] else [dollar] *([PercShare]/100)),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"PercShare"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Columns",null,"",Replacer.ReplaceValue,{"Team"})
in
#"Replaced Value"

Resources