Excel or SQL Server - Multiple columns to rows - sql

In Excel 2010 or Microsoft SQL Server 2012:
I have a table that has an ID in column a, and then columns B through (C - MM) with locations.
I want to have column A be all of the locations & then column B be the ID.
Any way I can figure out so far has been to transpose by hand.
Any help would be greatly appreciated!
Imaged added: image

here's a way that's semi-automatic in Excel which will work assuming the ID row is unique (if it isn't unique, you will need to clean that up first)
Assume your original worksheet of data is named "Raw Data"
Convert the raw data into a Table or Named Range - call it Table1
Add a new worksheet, call it "Normal Data" and type "ID" into cell A1 and "Location" into cell B1
Copy the data (no header) from column A (this is the list of IDs) from "Raw Data" into column A of "Normal Data"
Add this formula to column 2 of "Normal Data" as follows, but change "X" to the number of rows of data (excluding header) you have in "Raw Data":
=VLOOKUP(A2,Table1,ROUNDDOWN((ROW()-2)/X,0)+2,FALSE)
Make sure this looks correct
Append the entire "List of ID's" a few times to column 1 of "Normal Data" a couple of times
Make sure it still looks correct
If your location data extends to Column MN, then you need 350 total copies of the original ID list.
Save the Excel to something like "Normalize Location with Formula"
Save the excel to something like "Normalized Location Data"
Copy paste values of "Normal Data" to save values only, no formulae
Save the worksheet
Optionally filter the "Normal Data" to rows with blank location data and delete
This is now ready to import to a DB (and if you didn't delete the blanks, you cand just delete them via SQL after you import)
Why this works: Vlookup gets the correct row of raw data, and then the formula dynamically adjusts to the correct column based on the row count.

Pivoting rows into columns in SQL Server
Spioter, thanks for the info. I never tried this as i finally found information about using the UNPIVOT command in SQL.
Link is above.

Related

Index Match or Vlookup in a Power Query

I am trying to build a query that populates data in a column in a similar way to how an index, match or vlookup works.
My workbook has 2 sheets:
Contains open order data pulled from SQL (SQL_Data)
Contains saved comments that were manually entered on the SQL sheet (Preserved_Comments)
I have two queries set up:
Saves the order number and the manually input comments from the SQL_Data sheet, and puts them in a table on the Preserved_Comments Sheet
Refreshes the SQL data table
I have been trying to add a 3rd query that adds the comment column back into the SQL_Data sheet, but I can't figure out how to do a join / index, match / Vlookup that looks at the order numbers and populates the saved comment.
My goal for this is to create an order report where when the SQL data is refreshed, the manually added comments are saved and follow the order number.
Any guidance would be appreciated!
You can merge both Queries into a new one. Use a key column, It would be the lookup array in INDEX/MATCH. This should allow you to join the column.

generate a hyperlink in sheet linking to matched data found in a different sheet

I have two different sheets in a workbook with data, all of this data is organized by Site ID's. What I want to do is use these site ID's to create a hyperlink in one of the data sheets that when clicked takes the user to the corresponding Site ID in the other sheet. The sheet name where I want to write the hyperlinks to is called "Report_Manual" and I want to write hyperlinks down every row down the first column. The sheet that I am hyperlinking to is called "Data". The premise Ids are located in column C for sheet "Report_Manual" and column K in sheet "Data". Below is an a ttempt of what I was trying to generate in excel for a single cell formula however the issue I ran into for this formula is that the Premise ID's in sheet "data" are variant data type while the Premise ID's in sheet "Report Manual" are integer data type. This makes even a simple formula like the one below not get any matches as the data types being matched aren't the same.
=HYPERLINK("#"&CELL("address",INDEX(Data!K3:K580001,MATCH(C3,Data!K3:K580001,0))),C3)
If anyone has any idea of a macro to solve this issue that would be extremely helpful.
If the IDs in "Data" are text, try changing your match lookup value to text:
=HYPERLINK("#"&CELL("address",INDEX(Data!K3:K580001,MATCH(TEXT(C3,0),Data!K3:K580001,0))),C3)

Excel drop down list to convert full name & enter Abbreviations

I have a database called Overtime with a second sheet called data. On the first sheet I am using data validation to access the data list on the data page. At present The list has peoples full names. What I want to do is when a name is selected it then changes it to the Initials from the Data sheet. Not sure if this can be done into the one cell, or to use something like Vlookup to enter the initials into an adjacent cell. The names and Initials are on the same rows on the data page (in columns "I" & "J"). On the main database the validation list is for column "O". I have a column "P" ready to put in the formula to show the initials if this the only way it can be done.
With data in the Data sheet like:
In P1 enter:
=IF(OR(O1="",NOT(ISNUMBER(MATCH(O1,Data!I:I,0)))),"",VLOOKUP(O1,Data!I:J,2,FALSE))
and copy down.

Change an excel function into vba code

I have written 2 Excel functions to copy data to cell G12 when data is entered in cell F12
=IF(ISBLANK(F12)," ",(F12))
if data in the deleted from cell G12 it is copied to H12
=IF(ISBLANK(G12),(F12)," ")
these work perfectly but I was wondering if the same procedure can be carried out in vba on a dynamic range as I want to keep adding rows
To achieve your requirements, you can format your range as a table from Home>Format as Table. Now, assuming your table as two columns respectively named "name" and "non blank name", you can enter the following formula in the second cell of the second column:
=IF(ISBLANK([#Name])," ",[#Name])
As you add rows to the table, the formula will be copied automatically to the new row.

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].