Excel index match in SQL - lookup a value from a matrix - sql

I'm trying to perform somethign similar to an excel index match in SQL.
I have a matrix looking like the below:
Matrix in excel:
This is made by variable Pers (vertical) and variable Contribution (horizontal) where 5-6-7-8-9-10 are values the variable contribution can take. this is just for illustration purposes, my matrix is 72 columns per 165 rows for a total of 11644 cells (Universe of possible values).
Through those 2 variables I need to extract for any ID the cell 'value' inside the matrix. an example below of the desired output:
Output in excel:
I can make this in excel via index match but I would like to import this matrix in SQL and make the lookup in there so the calculation can be dynamic because I have always new IDs with their own 'pers' and 'contribution' that will need to be associated with a value in that matrix, therefore it's inefficient to extract the IDs from SQL to make the calculation in excel to then import it into SQL again.
obviously I can't use a 'case when' because I have 11644 cases which would be a suicide to write/read
is there anything in SQL that can perform anything similar to index-match?
Any suggestion is appreciated!

Related

Coloring Excel Cell based on a table condition

I am going through the users of a system and reviewing if they have appropriate role names. I then completed an excel table that looks abit like this:
I'm trying to turn the table into a more readable format. I have made a pivot that looks like this:
But I'm not sure how to highlight the cells to reflect the 'Access Appropriate? Yes/No' column. Ideally, it should be colored yellow if the 'Access Appropriate?' = 'No'. I'm thinking of using VBA, but was wondering if there is an easier solution using formulas or pivot table?
Your pivoted data isn't an actual excel pivot table, is it? I know what the x mean, but where do they come from?
Two possibilities come to mind if you want a flexible setup without VBA, aswell as an rather simple VBA-approach that uses an UDF.
Quick'n'dirty (really dirty) would be to
use 1/0 instead of yes/no (you could write that into a helper column with an if-function)
create a new pivot with ROLE_NAME for columns, USER_NAME for rows and SUM or MAX of [Access appropriate] for values
that means: instead of your x you will end up having 1 and 0. Empty cells will still be empty.
conditional format the value-range, e.g. If 1 then green If 0 then yellow if "" then Nothing
Alternatively, you could build your output-table with formulas like INDEX, MATCH and VLOOKUP-formulas.
An additional Key-Column with USERNAME&ROLE_NAME will be needed
conditional format the value-range
VBA: Provided your Rows are distinct a user defined function could do the following
read data into a recordset IF that hasnt been done already (meaning: declared on module-level, the first function call will fill it)
access the data in your recordset with a Recordset.Filter based on your input parameters - USERNAME and ROLE_NAME, in your case
output a certain Field.Value based on your input parameter - Access Appropriate in your case
conditional format the TRUE/FALSE values you get (since this can't easily be done inside an UDF)

Count unique string variants

There could be quite a simple solution to this, but I am trying to find the number of times a unique variant (i.e. non-duplicates) of a string appears in a column. However this string is only part of the text contained in a cell, and not the entire cell. To illustrate:
EuropeSpainMadrid
EuropeSpainBarcelona
AsiaChinaShanghai
AsiaJapanTokyo
EuropeEnglandLondon
EuropeSpainMadrid
I would like to find how many unique instances there are of a string that contains "EuropeSpain". So using this example, I would find that a variant of "EuropeSpain" appears only twice (given that the second instance of "EuropeSpainMadrid" is a duplicate).
A solution to this is to use pivots to summarise the data and remove duplicated; however given that my underlying dataset changes often this would require manual adjustments and corrections. I would therefore like to avoid adding any intermediate steps (i.e. PivotTables, other data sets etc) between my data and the counts.
UPDATE: I now understand to use wildcards to solve the first part of my question (counting the occurrences of "EuropeSpain"), however I am not yet clear on the second part of my question (how to find the number of unique occurrences).
Is there a formula or VBA code that could do this?
Using wildcards:
=COUNTIF(A1:A6,"="&"*"&C1&"*")
For without VBA but with some versatility, I suggest with Text in ColumnA (labelled), ColumnB labelled Flag and EuropeSpain in C1:
=FIND(C$1,A2)
in B2 copied down.
Then pivot A:B with Flag for FILTERS (and 1 selected), Text for ROWS and Count of Text for Sigma VALUES.
Apply Distinct Values if required (and available!), alternatively a formula of the kind:
=MATCH("Grand Total",E:E)-4
would count uniques.

Checking for same value in rows and calculate corresponding total

I have a large s/sheet. Values in column A correspond to values in column B, C & D.
I need to combine some rows which have same value in column A and automatically calculates total of value in column B in all corresponding rows.
Then i need to delete all unnecessary rows
Any ideas how i can do this with some code?
I think that you can use Power Query or VBA. Probably you will be able to achieve this with formulas, but it will be not flexible. With Power QUery you can combine data from multiple sources, clean and transform and even load directly into PowerPivot model. If you will have some detailed information please let me know. If you can upload the sample workbook with your data i will be able to provide you some more information.

Excel: SUMing values that fall in a range (Extending SUMIFS to use ranges of Criteria)

First post - please be gentle.
I have an interesting problem where I'm trying to convert a flat data table into a cumulative total based on two ranges (to create cumulative graphs of progress). Something that should be really easy to do.
I've managed to collate the data into a summary table (effectively a pivot table but for layout and charting purposes I cant use an actual pivot table).
(I'm too new to post images - sorry its not embedded)
Screenshot
In this screenshot I'm trying to show the 3 columns on the left as a Flat data table.
Columns A & B are text values that can be 'any' text (so I cant use wild cards).
Column C is the value I'd like to SUM.
Currently I'm using an SUMIFS statement to find the sum of the "Hours" when the "Week" label matches the values in "column A" and the "Department" label matches the value in "Column B".
I would like to change this equation to find the cumulative value for each week (so in this example cell G6 would be 14 - i.e. 4 from week 1 and 10 from week 2.)
I would like to try and avoid duplicating the entire table just to find the cumulative
All I really need to be able to do in this example is replace the equation in F5:
=SUMIFS($C$2:$C$15,$B$2:$B$15,$E5,$A$2:$A$15,F$4)
with
=SUMIFS($C$2:$C$15,$B$2:$B$15,$E5,$A$2:$A$15,$F$4:F$4)
eg Sum the hours of all the weeks that have come before. Again - i cant use wild cards and I cant use pivot tables.
I imagine I need to use some Array formulae but I cant find any online resources to help me.
Any help would be appreciated.
Your formula works in Cell F5, but in G5 just make it add F5.
F5 =SUMIFS($C:$C,$B:$B,$E5,$A:$A,F$4)
G5 =SUMIFS($C:$C,$B:$B,$E5,$A:$A,G$4)+F5
H5 =SUMIFS($C:$C,$B:$B,$E5,$A:$A,H$4)+G5
I5 =SUMIFS($C:$C,$B:$B,$E5,$A:$A,I$4)+H5
Also use the entire column reference A:A if you dont know the total number of rows.
If you cant do that you could make your read data into a table and then instead of the sum_range being some defined locations, you can use Table1[Week] = Column A instead.
Hope this helps
-Scheballs

Setup an Excel template so calculations are not dependant on a specific number of columns / rows

Problem Statement:
I'm creating a template for multi tiered complicated calculations in MS Excel that depend on a few input "n x 3" matrices.
It is really difficult to redesign the 15 sheets or so (200 ~ 300 lines each) every time I have a different "n" where "n" ranges from 3 to 900.
Goals:
I'd like to input the number of "n" in a cell subsquently changing all the other sheets in the workbook accordingly.
Avoid VBA as much as possible
How can I achieve these goals?
Note: I'm willing to answer any questions or comments concerning my issue
EDIT
"n" represents the number of columns / rows, if n = 3, all calculations will be for a 3 x 3 matrix. If n = 500, all calculations will be for a 500 x 3 matrix.
I want Excel to do the expansions / contractions of the rows automatically, so i do't have to do them myself accross hundreds of tables
In Excel 2007 turn your data matrices into tables.
This can be done by clicking on a matrix and then on the Insert tab select Table. The keyboard shortcut for this functionality is Ctrl-L or Ctrl-T. Multiple tables can exist on the same worksheet.
Once your data is marked as a table, the table will dynamically expand when new data is added.
Each table is automatically given a name, starting with Table1. The name can be change via the Table tools - Design tab.
In formulas each table can be referenced by it's name.
=SUM(Table1)
Each column heading in the table is also usable in formulas.
=SUM(Table1[Column1])
In versions of Excel prior to 2007, 'Dynamic named ranges' can be used.
These can be created via the Insert - Name - Define menu.
Give the 'Dynamic named range' a name (e.g. Table1) and enter a formula similar to the following assuming your matirx starts in cell A1:
=OFFSET(Sheet1!$A$1,1,0,COUNTA(Sheet1!A:A)-1,3)
If your matrix starts in cell D10 the formula would look like this
=OFFSET(Sheet1!$D$10,1,0,COUNTA(Sheet1!D:D)-1,3)
This formula excludes any column heading in the matrix. It selects data on a 'n x 3' basis.
In formulas each 'Dynamic named range' can be referenced by it's name.
=SUM(Table1)
You will need to review the layout of your worksheet as the dynamic named range works out it's number of rows by counting all items that appear in the first column of data.
If you have cells populated above and/or below your matrix they will be included in the calculation and the 'Dynamic named range' will include rows below your data matrix.
To see which cells are included in a 'Dynamic named range' or table press F5 and type in it's name, then click on OK.
Create defined names (Insert - Name - Define) that use OFFSET and COUNTA to make dynamic ranges. Instead of
=SUM(A1:A300)
use
=SUM(MyRange)
where MyRange is
=OFFSET($A$1,0,0,COUNTA($A:$A),1)
See also
Instead of calculating against A1:A300, you can calculate against A:A, which is the entire column.
You may have to bear in mind that you don't want other stuff in that column when you design your layout.
In more recent version of Excel, you can select sets of data and format them as a table, in which case you can use the table column, for example Table1[Column2].