Transform data into variable amount of columns - sql-server

I've been struggling with a challenge I've inherited which I think is possible but can't easily see what the solution would be. It's along the lines of the return rows as columns scenario (which I appreciate there are a lot of posts about already but I think this is slightly different). I thought a PIVOT would be useful but I don't need to aggregate any values. The metadata describing each document is defined at folder level so documentID = 1 should be tagged with the values 111, ABC, DEF and GHI.
So I have a table as follows:
Current Dataset:
The aim is to have the data presented as this instead so all the metadata tags for both the folders and document are stored in one row (ultimately I'll be exporting to CSV)
Desired Dataset

I have the same problem. Not worked out completely yet. I have only done it twice now manually. And planning on making a macro.
I work with vertical lookup and sorting and deleting data.
In your case sort the data based on metatag_value, trasnfer all the BBB, CCC, etc to new columns. then write a VLookup in the Metag_value2, etc columns at the AAA row.
Copy/paste the entire datasheet as values to get rid of the formulas, and then delete all the rows without AAA in them.

Related

SQL Server - Mapping table to identify fields to sum

I have a large database of fields around 400, that need to be summed in various different ways.
Currently I do this in Excel with a look up table to identify which field to sum and then use a sum if formula to then sum those columns. How would I replicate this in SQL?
I've seen some examples where you manually type out each field to calculate but this seems very impractical if you are summing up to 300 fields and if there are any changes to that mapping table, then we would have to redo that.
So far my only solution is to copy and paste all the fields to be calculated in excel, and add the correct summation syntax and paste back in to SQL.
Thanks
Lots of ways of doing this;
create a case statement which acts like your sumif and pass the conditions in the case e.g. CASE WHEN amount >=100 THEN SUM(Amount) END as 'New Column'
Create a function and store all your logic in the function similar to above but easy to maintain should your logic change.
if you post your sumif's with a sample I can help you.

Can I Fix the a lookup field in Access database based upon a calculation or another field?

I am trying to create a database with field descriptions for a very large excel file that I have at work. I have created 3 tables- List of sheets, list of variables(including a lookup field pointing at the List of sheets table, so that I can select the sheet to which the variable belongs), and a third table which specify some validation rule.
In this third table, I want to see two lookup fields, one specifying the sheet in which the rule applies(say, 'Select Sheet'), and another specifying the variable(say, 'Select Variable'). I can point to the two different tables, but I want to do something a bit more nuanced than that. When I give a particular sheet name to 'Select Sheet', I want the lookup field for the variable('Select Variable') to show me only those variables which exists in that sheet.
I know that there probably will be solutions using forms, but this database is going to be very detailed and there are things to do afterwards, so I do not want to get into queries and forms before all data has been recorded in tables in a neat manner.
I have a good grasp of VBA in the context of excel and I am given to understand that I can extend the applications of Access using VBA. I am ready to do that, but I want to see before that whether this is some functionality of access that I am missing. Had anyone done anything similar before, and if so, did it take VBA to do it?

Writing array to filtered and grouped table fails

I am having trouble writing an array defined in VBA to a range in a table that has been grouped and filtered.
Writing an array to a table range is done using:
Sheets(1).ListObjects(1).ListRows(1).Range = MyArray
This works fine on a filtered table (i.e. filtering the rows using the filter buttons in the row headers). It also works fine in a grouped table (i.e. with some columns grouped using Data --> Outline --> Group.
However, when combining this, the array is not written to the cells in a correct manner. An array [1,2,3,4,5,6,7,8] is written as [1,2,3,1,2,3,4,5], if I group columns 2 and 3 in the table. This is NOT the case, if I haven't also filtered the table.
Do anyone have an idea what's going on here? An example workbook can be found here. It contains a simple table and one sub that tries to write an array to the table.
Any help would be greatly appreciated!
BR
Torben
OK, so I have figured out a workaround/verification that this is a bug in Excel. If you select a cell outside the table range before inserting the array, it works fine - even when both filtering and grouping is active. Why this is the case, I have no idea. It's a clumsy solution (I'm not fond of selecting cells through VBA), but it works.
(Sorry, I originaly posted it as a comment, and not an answer).
If anyone has a more elegant solution, I would still be very interested in seeing it.

How to build a column using data from another table with contraints

I have a very-badly done Excel Database that I have to repair. To do that, I need to get specific data from a table and put it in another table, if certain cells in each rows equals cells from rows in the first table.
Here is a screenshot to explain what I need.
I tried with advanced filters but I can't get it to work.
I got an answer.
The trick here is to use a vertical search (=VLOOKUP(cell; tableToSearch; cellToSearchLocation) and IF conditionning.
Sadly, my example isn't valid because the cell we search (in "hey" column) is a in a column before the "ho" and "hi" columns.
If anyone has any questions about this solution, feel free to ask.

Excel dynamic offset table with results criteria

I am looking for a way to make a dynamically updated table in excel using a linked data source with filtered results.
What I currently have is a form control scrollbar that is linked to a random cell. That random cell drives an offset function (below) to pull results from the linked data table in another worksheet. All of this works wonderful but I am looking to strip out rows that meet a certain criteria, which I already have calculating to a true/false result in the table itself to make it simple. What I cannot figure out is how to modify my current offset lookup to filter out the results that are false in my calculated column.
=OFFSET(Sheet1!$C2,$I$99,0,1,1)
Sheet1 is where the linked data table is located.
$I$99 is where the cell link for my scrollbar is.
As the scrollbar moves the value in the cell link changes thus updating the offset lookup down the table. I should also state there are 14 rows in my table as well. I will need to filter out the false results but also in turn omit the blank rows as there are over 400 rows in the source table and only about 30 true results in the calculated column.
I unfortunately cannot provide the workbook I am using as it has company sensitive data in it but if it is absolutely needed I can create a filler document with made up data to provide something to work with.
I have looked at advanced filters already and they will not do the trick as they are not dynamic in nature. I would like a non VBA solution as this is a dashboard project that I will be distributing to my team here at work and macros are disabled on our network for security reasons every time an excel is opened so it will become a nuisance.
Let me know if you all need anything else from me.
Figured it out with a little help from a friend, coded each cell to function as an array part and it works like a charm.
{=IFERROR(INDEX(Dashboard_Data[Summary],SMALL(IF(LEFT(Dashboard_Data[Stale],1)="T",IF(Dashboard_Data[Visible],ROW(Dashboard_Data[Visible])-1,""),""),ROW(A1)+$K$18)),"")}

Resources