Count date range based on criteria - excel

Let´s suppose I have generated a report with dates (day/month/year) when soccer teams won titles. This is how the report is going to look like:
Area
Team
Champions League
Europe League
England
Chelsea
27/01/2021
15/01/2021
Spain
Real Madri
27/02/2021
20/01/2021
Spain
Barcelona
18/02/2021
France
PSG
27/03/2021
27/02/2021
My objective here is going to count how many titles each area won per month. So, this is how my desired output looks like:
Area
January
February
March
England
2
Spain
1
2
France
1
1
What I tried to do was the following (for January and England):
=COUNTIFS(Table[[#All],[Champions League]:[Europe League]],">01/01/2021",Table[[#All],[Champions League]:[Europe League]],"<31/01/2022",Table[[#All],[Area]],"=England")
However, my output using this formula is "#VALUE!". Can you please help trying to figure out what I am doing wrong?

You may try something like this as shown in image below, so I have used the incredibly versatile SUMPRODUCT Function to achieve the expected output
Formula used in cell B8 Fill Down & Fill Right
=SUMPRODUCT(($A8=$A$2:$A$5)*(B$7=TEXT($C$2:$D$5,"mmmm"))*($C$2:$D$5<>""))
To hide the zeros, you may custom format the cells by pressing CTRL 1 --> Format Cells --> Number Tab --> Category --> Custom --> Type --> 0;;
EDIT
Approach Using Power Query
let
Source = Excel.CurrentWorkbook(){[Name="Table39"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Area", type text}, {"Team", type text}, {"Champions League", type date}, {"Europe League", type date}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Area", "Team"}, "Attribute", "Value"),
#"Extracted Month Name" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Value", each Date.MonthName(_), type text}}),
#"Grouped Rows" = Table.Group(#"Extracted Month Name", {"Area"}, {{"All", each _, type table [Area=nullable text, Team=nullable text, Attribute=text, Value=text]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All],"Index",1,1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Value", "Index"}, {"Value", "Index"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"All"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Value]), "Value", "Index", List.NonNullCount)
in
#"Pivoted Column"
Using Power Query will be dynamic, one time operation and every time add new data to the original source the imported table from Power Query gets updated within few by a single refresh, please follow the steps
• First select any cell within the range,
• Then from Data Tab under Get & Transform Data, Click From Table/Range,
• A pop up shall appear, check mark the My table has headers and press Ok
• Data gets loaded into Power Query Editor
• Change the name of the table from Query Settings in the right hand under Properties with CountTbl
• Now select any column and press CTRL A
• If the Changed Type Step is already showing in Applied steps then you don't have to follow this, please goto the next step
From Transform Tab --> Click Detect Data Type
• Now you will find the dates are still showing as Time Stamps therefore select both Champions League & Europa League Columns and from Transform Tab --> Click Data Type from Any to Date only, you shall get a message asking whether to Replace the current step or to add new step, you need to select the Replace
• Well data types changed, now select the Area and Team Columns and press Right Click Unpivot Other Columns also you may find the option from Transform Tab --> Unpivot Columns dropdown and select Unpivot Other Columns
• Next, select the Value column showing dates --> Goto Transform Tab --> From Date & Time Column Group --> Click Date --> Month --> Name Of Month
• Again select the Area Column --> From Transform Tab --> Group By --> Give any fancy name to the New Column Name --> Operation --> All Rows and press OK
• Now goto Add Column Tab --> Click Custom Column and enter this following Formula or the M-Code in custom column formula space
Table.AddIndexColumn([All],"Index",1,1)
press Ok
• So from the custom column newly created, click on the dropdown, and uncheck the box use original column name as prefix and select the columns Value and Index and press Ok
• Remove the column which we created by Group i.e. All --> Select the column and press delete key from your keyboard
• Now select the value column which will be showing the month name and goto Transform Tab and click Pivot Column
• There Values Column needs to be Index and in Advanced Options you to select Count (Not Blank) and press OK
• You shall see the expected output has been achieved!!!
• From Home Tab Click Close & Load To --> Import Data --> Table --> You can either select Existing Worksheet (enter the cell reference where you want to place the imported table) or New Worksheet and press Ok
IMPORT DATA
• Done Full_Explanation Power_Query

COUNTIFS doesn't like your dissimilar sized criteria ranges.
You would be better served setting your data up in a tabular format like so:
Area
Team
Championship
Date
England
Chelsea
Champions League
1/27/2021
England
Chelsea
Europe League
1/15/2021
Spain
Real Madri
Champions League
2/27/2021
Spain
Real Madri
Europe League
1/20/2021
Spain
Barcelona
Europe League
2/18/2021
France
PSG
Champions League
3/27/2021
France
PSG
Europe League
2/27/2021
You get your data into this format easily by using Power Query. Simply load the data in, select the two date columns, and Unpivot.
And then you could use a simple Pivot Table to display the data in your preferred format:
To turn the full date to just the Month name select one cell > right-click > Group > by Month

Related

Summing, Lookups and multiple sheets in Excel

I'm not a native Excel user (much more of a SQL man) and I have the following scenario that is doing my head in. Mainly because I'm sure it's relatively simple, but because I'm not super-familiar with all the advanced functions of Excel.
I have a 2 sheets in question.
Sheet One has the following columns:
SKU
Price
1234
$10
1235
$20
Sheet Two has the following Columns:
SKU
Business Unit
1234
BU1
1235
BU1
1234
BU1
1234
BU2
1234
BU2
1234
BU2
And I have the following Formula:
=SUMIF('Sheet1'[SKU], VLOOKUP($F$2, sheet2, 2, FALSE), 'Sheet1'[Price])
(Which admittedly is copy-pasta from the Internets and then I've tried to mash together to get it to do what I want)
What I am trying to do is grouping by Business Unit, look up the SKUs and multiply the total, based on Business Unit by the Price - so it would look like the following:
Business Unit
Total Value
BU1
$40
BU2
$30
And my limitations in Excel are causing my hair to fall out as I bang my head against my keyboard - as I'm sure it's relatively simple - but I'm missing something key.
You may try as shown in below as well,
• Formula used in cell G2
=LET(_merge,DROP(HSTACK(A3:B8,XLOOKUP(A3:A8,D3:D4,E3:E4)),,1),
_uBUnit,UNIQUE(INDEX(_merge,,1)),
_tValue,BYROW(_uBUnit,LAMBDA(x,SUM(INDEX(_merge,,2)*(INDEX(_merge,,1)=x)))),
VSTACK({"Business Unit","Total Value"},HSTACK(_uBUnit,_tValue)))
Notes: Break-down & Explanation Of Each.
• _merge --> Returns both the tables as combined after looking the price for each SKU and then excludes the SKU from the array, only keeping the one required as output, i.e., Business Unit & Price
XLOOKUP() --> Looks Up On SKU To Return The Price.
HSTACK() --> Used To Combine Both The Arrays.
=HSTACK(A3:B8,XLOOKUP(A3:A8,D3:D4,E3:E4))
Using DROP() --> To Exclude The SKU Col.
DROP(HSTACK(A3:B8,XLOOKUP(A3:A8,D3:D4,E3:E4)),,1)
• _uBUnit --> Returns the unique value of each Business Unit.
UNIQUE(INDEX(_merge,,1))
• _tValue --> Returns the Total Values of each Business Unit
BYROW(_uBUnit,LAMBDA(x,SUM(INDEX(_merge,,2)*(INDEX(_merge,,1)=x))))
• Lastly we are packing the whole thing, within a VSTACK() & HSTACK() to get the required output.
VSTACK({"Business Unit","Total Value"},HSTACK(_uBUnit,_tValue))
Please suit the data ranges accordingly with your data.
You can also perform such tasks quite easily using Power Query as well:
To accomplish this task using Power Query please follow the steps,
• Select some cell in your Data Table,
• Data Tab => Get&Transform => From Table/Range,
• When the PQ Editor opens: Home => Advanced Editor,
• Make note of all the 2 Tables Names,
• Paste the M Code below in place of what you see.
• And refer the notes
let
//Source Table -- SKUtbl
SourceOne = Excel.CurrentWorkbook(){[Name="SKUtbl"]}[Content],
DataTypeSourceOne = Table.TransformColumnTypes(SourceOne,{{"SKU", Int64.Type}, {"Business Unit", type text}}),
//Source Table -- Pricetbl
SourceTwo = Excel.CurrentWorkbook(){[Name="Pricetbl"]}[Content],
DataTypeSourceTwo = Table.TransformColumnTypes(SourceTwo,{{"SKU", Int64.Type}, {"Price", Int64.Type}}),
//Merging Both Tables
MergeTables = Table.NestedJoin(DataTypeSourceOne, {"SKU"}, DataTypeSourceTwo, {"SKU"}, "Pricetbl", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(MergeTables, "Pricetbl", {"Price"}, {"Price"}),
//Removing the SKU Column
#"Removed Columns" = Table.RemoveColumns(Expanded,{"SKU"}),
//Grouping By Business Unit
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Business Unit"}, {{"Total Value", each List.Sum([Price]), type nullable number}})
in
#"Grouped Rows"
• Change the Table name as BusinessUnittbl before importing it back into Excel.
• When importing, you can either select Existing Sheet with the cell reference you want to place the table or you can simply click on NewSheet
There are more than one way to achieve this. If you have Office 365, then you can try following. I have set it up on one sheet as below.
Formula in Blue Cell G2 is
=UNIQUE($B$2:$B$7,FALSE)
Formula in Gold Cell H2 is
=SUM(LOOKUP(FILTER($A$2:$A$7,$B$2:$B$7=$G2),$D$2:$D$3,$E$2:$E$3))
You will have to adopt this to suit your sheet/data structure.

A Frustrating Set of Variables in Excel

My industry (aftermarket auto components) utilizes a data standard for digital distribution, and I am currently attempting to create a living reference document, formatted with the correct information in the correct way, to make updating our standard database a less time consuming process.
My company has a 'Master Data Sheet' which contains every piece of data for all of the 20k+ products that we sell. All of our pricing and tracking sheets call cells or ranges from the Master Sheet, in addition to most of our front-facing web presence.
Here's my problem. The standard requires that our marketing descriptions be broken into separate lines with a specific identifier code and grouped by item ID:
Item ID Desc Code Desc
CHD001A AAA Brake Kit
CHD001A BAA Cross-drilled...
CHD001A BAA All of our...
CAE221B AAA Replacement Part
CAE221B BAA Reinforced with...
Our Master Data sheet has a different structure:
Item ID Desc - AAA Desc - BAA Desc - BAA
CHD001A Brake Kit Cross drilled... All of our...
CAE221B Replacement Part Reinforced with...
I'm completely stuck on how to get the right info into the right slots. I CANNOT alter the structure of the Master Sheet or I will have to remap at least thirty other spreadsheets. A VLOOKUP won't work in the horizontal way it needs to, and IF statements will get 20 nests in and then lack have a good way to group things. Please help.
Assuming that your task is to find the description of item CH001A in the master db, and that you know the description code, you can use INDEX/MATCH. Here's the setup I used for developing the formula.
I created a simulation of your master in A1:D4 (one row more than the example in your question.
I assigned G2 as the cell where I would enter the Item ID and G3 to enter the Desc Code.
Now the formula =IFERROR(MATCH(G2,A1:A4,0), 1) finds the sheet row number by Item ID and =IFERROR(MATCH("Desc - " & G3,A1:D1,0),1) finds the sheet column number by Desc code. Note that both formulas default to 1 if not found.
Now the formula below will return the description.
=INDEX(A1:D4,IFERROR(MATCH(G2,A1:A4,0), 1),IFERROR(MATCH("Desc - " & G3,A1:D1,0),1))
Observe that the db range A1:D4 includes the captions and both range A1:A4 and A1:D1 start from the extreme top or left. This enables a column or row caption to be displayed in case of error (when an Item ID or Desc Code isn't found).
The formula isn't perfect yet but the method is. I take it that you will be able to tweak it to optimize adaptation to your needs. Let me know if you need help or advice with that.
Very simple to do with Power Query, available in Excel 2010+
Select some cell in the Master Table
If this is not a real Table, it will be changed into one
Data / Get&Transform / From Table/Range
In the PQ editor window that opens, select the Item Id column
Unpivot other columns
Split the resultant Attribute column by `Transition from non-digit to digit
This will get rid of the automatically created suffixes caused by creating
a table with initially identical column headers
If there are digits in the code itself, you'll need to remove the terminal digit(s) using a custom column with a formula.
The best way to do that would depend on the actual structure of your values
Delete the Attribute.2 column (the one with the terminal digits)
Rename the columns appropriately
Here is the generated M Code.
You can just paste this into the Advanced Editor of PQ. If you do, be sure to change the Table Name in Line 2 to whatever your real table name for the Master Data turns out to be.
let
Source = Excel.CurrentWorkbook(){[Name="Master"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Item ID", type text}, {"Desc - AAA", type text}, {"Desc - BAA", type text}, {"Desc - BAA2", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Item ID"}, "Attribute", "Value"),
#"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Attribute.1", "Desc Code"}, {"Value", "Desc"}}),
#"Filtered Rows" = Table.SelectRows(#"Renamed Columns", each ([Desc] <> ""))
in
#"Filtered Rows"

Calculate Overlap in Excel Dataset

I have an Excel file with ~500,000+ rows of data, each of which has (amongst other things) an ID and a certain value I'd like to manipulate. I'll use an example consisting of names and foods. The data looks something like this:
Name Food
Alex Melon
Alex Burger
Bruce Apple
Charlie Water
Alice Apple
Bruce Melon
Bruce Apple
Bruce Plum
I'd like to find the overlap in foods between any pair of names, giving me a result that would tell me (for example) that for the pairing of Bruce vs Alex, 2/3 of Bruce's data is unique and 1/3 is the same is Alex's list, whilst for Alex 1/2 his data in unique and 1/2 is the same as Bruce.
There is no consistency in the amount of foods a person can have listed alongside their name. And its entirely possible for some people to have foods not found alongside anyone else.
I thought to present it through something like this, where each percentage sign is the overlap for that pairing (read by row, so C2 would be the proportion of Alex's data also found in Alice's, whilst B3 would be the proportion of Alice's data also found in Alex's):
Alex Alice Bruce Charlie
Alex - % % %
Alice % - % %
Bruce % % - %
Charlie % % % -
I've been struggling to think and find a formulae or VBA script that would achieve this and calculate the overlap. I've considered creating (i) a helper column that concatenates the name and food, (ii) a new de-duplicated unique list of foods and maps this against the helper column. However, as far as I can tell, whilst that will help me summarise which foods go with which person, it won't help me find out the overlap between each person's list of foods.
Any help would be greatly appreciated!
This was fun!
You can use Power Query for this.
Highlight your data and insert a table, make sure you say the first row is columnm headers.
Go to the Table ribbon and change the table's name to "PersonTag"
Go to the Data ribbon and in the 'Get & Transform' section, click "From Table"
This opens up Power Query, with a new query called "PersonTag"
Highlight both columns, then on the "Home" ribbon, choose "Remove Rows - Remove Duplicates"
In the "Home" ribbon, click "Manage - Reference"
You've just created a new query that refers to the PersonTag query! Rename it to PersonCount.
Highlight the "Name" column, and in the "Transform" ribbon, click "Group By" and group by name, creating a new column called "PersonCount" that is the count of the rows.
Go back to editing the PersonTag query
Create a new query (or copy an existing one, it doesn't matter how), name it "PersonTagPersonTag", and then go to the "Home" ribbon, click "Advanced Editor" and replace whatever's there with the following.
let
Source = PersonTag,
// Recursion! Now each row contains it's own "PersonTag" table!
#"Added Custom" = Table.AddColumn(Source, "2nd", each PersonTag),
#"Expanded 2nd" = Table.ExpandTableColumn(#"Added Custom", "2nd", {"Name", "Food"}, {"2nd.Name", "2nd.Food"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded 2nd",{{"2nd.Name", type text}, {"2nd.Food", type text}}),
// We only want rows where the foods match and the names don't
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Name] <> [2nd.Name] and [Food] = [2nd.Food]))
in
#"Filtered Rows"
Now we're going to group by Name and 2nd.Name to get the row count of matches in a "PersonPersonCount" columns, bring in the PersonCount query we created earlier to get the total foods each Name, then create a PercentMatch column by dividing PersonPersonCount by PersonCount. Then we can get rid of the Count columns because we don't need them, and pivot by 2nd.Name! Create another query (I named mine "PersonvPerson").
let
Source = PersonTagPersonTag,
#"Grouped Rows" = Table.Group(Source, {"Name", "2nd.Name"}, {{"PersonPersonCount", each Table.RowCount(_), type number}}),
// Bring in PersonCount query
#"Merged Queries" = Table.NestedJoin(#"Grouped Rows",{"Name"},PersonCount,{"Name"},"PersonCount",JoinKind.LeftOuter),
// If you click the column type icon in the column title in the previous step, you get the dialog box you can fill out that does this step for you
#"Expanded PersonCount" = Table.ExpandTableColumn(#"Merged Queries", "PersonCount", {"PersonCount"}, {"PersonCount"}),
#"Added PercentMatch" = Table.AddColumn(#"Expanded PersonCount", "PercentMatch", each [PersonPersonCount] / [PersonCount]),
#"Changed Type" = Table.TransformColumnTypes(#"Added PercentMatch",{{"PercentMatch", Percentage.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"Name", "2nd.Name", "PercentMatch"}),
#"Sorted Rows" = Table.Sort(#"Removed Other Columns",{{"2nd.Name", Order.Ascending}}),
// I did this by highlighting the "2nd.Name" column, going to the "Transform" ribbon, and clicking "Pivot Column"
#"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[#"2nd.Name"]), "2nd.Name", "PercentMatch", List.Sum)
in
#"Pivoted Column"
Exit the Power Query window and keep your changes. By default, creating new queries automatically create new tabs on the worksheet that contain the data. Delete the tabs you don't want to keep.
If you're anything like I was a few months ago, your jaw is dropping at what you can do with power queries. I gave you the code because I was too lazy to tell you every little click to create the code, but don't be overwhelmed!!! I just clicked around to create each next step and it created the code for me! They made it easy to click around and try/undo things.

How to use Countifs,Or and Sumproduct efficiently

I have a list of accounts with 2 digit modifiers. Some accounts will have more then one modifier. I am looking for accounts with a certain combinations of modifiers.
So I have a list of accounts in the B column.
I have the modifiers in C Column
Example
Act # Modifier
111 80
111 56
111
222 55
222
333 51
333 50
333
I have some working code that works great until I get to many rows.
In this sample formula I have 8 Modifier groups.
50,22,51,62
51,22,62
54,50,51
55,50,51
56,50,51
80,50,51
"AS",50,51
59,50
=IF(OR(SUMPRODUCT(COUNTIFS(B:B,B3,C:C{50,22,51,62}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{51,22,62}))>=2,SUMPRODUCT(COUNTIFS(B:{54,50,51}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{55,50,51}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{56,50,51}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{80,50,51}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{"AS",50,51}))>=2,SUMPRODUCT(COUNTIFS(B:B,B3,C:C,{59,50}))>=2),"Check","")
This code will put check by any account that has 2 or more of the modifiers from any of the 8 groups. It has to be 2 modifiers from the same group though.
I was just wondering if there is a better way to write this? Instead of doing all these or can I just do OR for the different modifier criteria I am looking for?
Something like
=COUNTIFS(H:H,H5,I:I,OR({59,50},{"AS",50,51}))
As requested by #SkysLastChance, I will post my solution using Power Query (PQ) even though the question was tagged to Excel-Formula.
Please note you MUST use Excel 2010 or later versions otherwise you will not be able to use Power Query. My answer might not be robust enough for people who has not used PQ before. So feel free to leave a question if you are unclear with any particular step.
Step 1
Convert the Account List and Modifier Group in the example into Table in your excel worksheet. One way of doing that is to highlight the data including headers and press Ctrl+T. Then you should get two tables as shown below. I have named the first table as Tbl_ActList, and named the second one as Tbl_MoGrp.
Please note I have added some data to the Account List table for result testing purpose.
Step 2
Select any cell within a table, go to the Data tab on top of your excel (mine is Excel 2016), click From Table in the Get & Transform section. It will load and add the table to the built-in PQ Editor. You can exit the editor (and keep the changes), and repeat this step to add the second table to the PQ Editor. Alternatively you can add a new query in the PQ Editor and find the second table from your excel worksheet. I will not demonstrate this process as you can google the know-how later on.
Step 3
Once you have added both tables to the editor, you can start editing/transforming data in each table/query using built-in functions and/or advanced coding. In this case I only used built-in functions.
For the Modifier Group table, I want to transform the original data into a 2-Column list with one column showing which Group the modifier belongs to, and the other column showing a single modifier.
Firstly, use the Split Column function in the Transform tab to split the original modifier groups into single value by using , (comma) as the delimiter.
The new table is in matrix structure which is no ideal for look up purpose, so I used Unpivot Columns function in the Transform tab to convert it into list structure. What I actually did is to highlight the Grp column and select Unpivot Other Columns to get the list. Alternatively you can highlight the first four columns and use Unpivot Columns to get the same list.
Then I renamed Value column as Modifier, and removed the Attribute column to end up a 2-Column table.
Please note all data in each table/query in this example have been set to 'Text' format (aka data type). Data type is very sensitive and specific in PQ, and incorrect data type may lead to error.
Here is the full code behind the scene. All steps are performed using the built-in functions without any advanced coding:
let
Source = Excel.CurrentWorkbook(){[Name="Tbl_MoGrp"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Modifier", type text}, {"Grp", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Modifier", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Modifier.1", "Modifier.2", "Modifier.3", "Modifier.4"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Modifier.1", type text}, {"Modifier.2", type text}, {"Modifier.3", type text}, {"Modifier.4", type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Grp"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Value", "Modifier"}}),
#"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Attribute"})
in
#"Removed Columns"
Step 4
With the Modifier Group list ready, we can look up the modifier group in the Account List table for each modifier using Merge Queries function in the Home tab. The logic is to find the link between two tables to conduct a look up.
Firstly, select/highlight the column (Modifier) that contains the look up value in the origin table (Tbl_ActList), then select the table (Tbl_MoGrp) that you want to look up from, then select/highlight the corresponding column (Modifier) in the second table, and then click OK to continue.
Please note before merging I have filtered the Modifier column in the Account List table to get rid of cells showing null (blank) as they are not useful for the look up.
After merging the queries there is a new column added to the Account List table. It may look like a column but it contains all data from the Modifier Group table stored in Grp column and Modifier column. As we want to look up the modifier group only, we can Expand the column to show the Grp column only.
Click on the little square box on the right hand side of the header of the last column to trigger the Expand function, then select the Grp column only and click OK to continue.
Now we have a table showing account number, modifier, and modifier group. We can then use the Group By function in the Home tab to find out for each account number how many modifiers have appeared in each applicable modifier group.
Please See below screenshot for the settings for the Group By function.
Then I sorted the table ascending by Acc # column, and filtered the Count column to show values greater than or equal to 2, i.e. at least 2 modifies linked to that account number have appeared in a modifier group.
Here is the full code behind the scene:
let
Source = Excel.CurrentWorkbook(){[Name="Tbl_ActList"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Act #", type text}, {"Modifier", type text}}),
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Modifier] <> null)),
#"Merged Queries" = Table.NestedJoin(#"Filtered Rows", {"Modifier"}, Tbl_MoGrp, {"Modifier"}, "Tbl_Grp", JoinKind.LeftOuter),
#"Expanded Tbl_Grp" = Table.ExpandTableColumn(#"Merged Queries", "Tbl_Grp", {"Grp"}, {"Grp"}),
#"Grouped Rows" = Table.Group(#"Expanded Tbl_Grp", {"Act #", "Grp"}, {{"Count", each Table.RowCount(_), type number}}),
#"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Act #", Order.Ascending}}),
#"Filtered Rows1" = Table.SelectRows(#"Sorted Rows", each [Count] >= 2)
in
#"Filtered Rows1"
Step 5
The answer could stop at Step 4 as the table has shown the account number that we are looking for. However if there are thousands of account numbers, then it is better to Remove Other Columns except the Act # column, and Remove Duplicates within the column, and then Close & Load the result to a new worksheet. The final result may look like this:
A tip here, before Close & Load any query for the first time, it is better to set the following in your Query Options. It will prevent PQ Editor to load each of your queries to a separate worksheet by default. Just imaging how long it will take if you have 20 queries in your PQ Editor and each of them have more than a thousand lines of data.
Once you change the default option, PQ Editor will only create connections for your queries after you click Close & Load, and you can manually load a specific query result to a worksheet as shown below:
Conclusion
I believe if this question was tagged as a PowerQuery, there may be more concise or 'fancier' answers than mine. Regardless, the things that I like PQ the most are it is a built-in function of excel (2010 and later versions), it is scalable, replicable and more powerful when it comes to data cleansing and transforming.
Cheers :)

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"

Resources