Converting specific columns to vertical one record

dankar

Board Regular
Joined
Mar 23, 2016
Messages
113
Office Version
  1. 365
Platform
  1. Windows
I have similar to this table with many columns and thousand of rows (patients)

- as you can see each patient have 6 tests(test1 to test6) with different 6 result.
- all other columns have the same result.
- I need to switch the results from vertical to horizontal so I the table can show one record for each patient. like table2

Table 1:

Before:
FILE NO
RCVD DATE
COUNTRY
ID_NO
PID_NO
TEST
RESULT
REPORT DATE
1
5/1/2018
USA
1234567890
ABCDEFGH
TEST1
NOT TESTED
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH
TEST2
NOT TESED
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH
TEST3
POSITIVE
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH
FINAL RESULT
POSTTIVE
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH
TEST4
NOT TESTED
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH

TEST5
NOT TESTED
5/25/2018
1
5/1/2018
USA
1234567890
ABCDEFGH
TEST6
PENDING
5/25/2018

<tbody>
</tbody>

Table 2

After:
FILE NO
RCVD DATE
COUNTRY
ID_NO
PID_NO
TEST1
TEST2
TEST3
TEST4
TEST5
TEST6
FINAL RESULT
REPORT DATE
1
5/1/2018
USA
1234567890
ABCDEFGH
NOT TESTED
NOT TESTED
POSITIVE
NOT TESTED
NOT TESTED
PENDING
POSITIVE
5/25/2018

<tbody>
</tbody>
 
thank you for the links.
I have one more question plz.
If I want to add more columns to your code, I need to type the column name for each one in {"COLUMN NAME", type text} or there is another way like to select the fields and then excel transfer it to code

Thank you
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
If I want to add more columns to your code

You mean what?

you've 8 columns so where you want to add more columns?

FILE NORCVD DATECOUNTRYID_NOPID_NOTESTRESULTREPORT DATE
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST1NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST2NOT TESED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST3POSITIVE
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHFINALRESULTPOSTTIVE
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST4NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST5NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST6PENDING
25/05/2018​
 
Last edited:
Upvote 0
you can add more rows in source table, like (yellow)

FILE NORCVD DATECOUNTRYID_NOPID_NOTESTRESULTREPORT DATE
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST1NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST2NOT TESED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST3POSITIVE
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHFINALRESULTPOSTTIVE
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST4NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST5NOT TESTED
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST6PENDING
25/05/2018​
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST7PENDING
25/05/2018​
2​
06/01/2018​
USA
1234567891​
ABCDEFGHTEST8NOT TESTED
26/05/2018​

firsrt yellow has only TEST added , the rest is the same
second yellow has TEST added and all the rest is different so in the result you will get two rows
just add what you want and refresh green table or use Ctrl+Alt+F5

FILE NORCVD DATECOUNTRYID_NOPID_NOTEST1TEST2TEST3TEST4TEST5TEST6FINALRESULTREPORT DATETEST7TEST8
1​
05/01/2018​
USA
1234567890​
ABCDEFGHNOT TESTEDNOT TESEDPOSITIVENOT TESTEDNOT TESTEDPENDINGPOSTTIVE
25/05/2018​
PENDING
2​
06/01/2018​
USA
1234567891​
ABCDEFGH
26/05/2018​
NOT TESTED

I'll be back in a few hours so if you've more questions try describe in detail what you want to achieve
 
Last edited:
Upvote 0
the excel table was exported from access query.(contains the main columns I need)
I was asking if I created a bigger table which included these columns and another ones also.

I will use your code and alter it to add the new columns, so I was asking do I need to type each new columns in the same format you used,or there is an easier way..like for example selecting the columns and excel will translate it the code you gave me..
thank you
 
Upvote 0
with new columns

FILE NORCVD DATECOUNTRYID_NOPID_NOTESTRESULTREPORT DATENewColumnNewColumn1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST1NOT TESTED
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST2NOT TESED
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST3POSITIVE
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHFINALRESULTPOSTTIVE
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST4NOT TESTED
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST5NOT TESTED
25/05/2018​
DATADATA1
1​
05/01/2018​
USA
1234567890​
ABCDEFGHTEST6PENDING
25/05/2018​
DATADATA1

result

FILE NORCVD DATECOUNTRYID_NOPID_NOTEST1NewColumnNewColumn1TEST2TEST3TEST4TEST5TEST6FINALRESULTREPORT DATE
1​
05/01/2018​
USA
1234567890​
ABCDEFGHNOT TESTEDDATADATA1NOT TESEDPOSITIVENOT TESTEDNOT TESTEDPENDINGPOSTTIVE
25/05/2018​

why don't you test it?! It's easy, even with MS Access

I strongly recommend to read information from links in previous post and here also: Unpivot Columns
 
Last edited:
Upvote 0
I'm really grateful for your help.

Surly I will read the links you sent, but the issue for now that I need to show the result tomorrow.

I already tested the adding more columns before I see your reply ;) :)
I just fallowed your code and added the new columns.


Honestly I needed this to be solved in Access but no one gave me a solution in the access forum except you when I posted it here.

Can I use the same code in access ? please can you explain to me how to do it in Access .

again thank you so much!
 
Upvote 0
No, You can't use this code IN MS Access directly

You can get table from access db via Power Query and use appropriate M-code and on the end show result in the sheet
 
Upvote 0
I already posted it in this link but no one gave a correct answer,its ok to use Excel.

sorry to bother you again.

I noticed in my data result that there is another field that have different results.

in my main example : "REPORT DATE " could also have different entries like the "Result" which preventing from having one record for every patient.

I tried to add it to your code but I got an error :

"Expression.Error: We cannot convert the value "REPORT DATE" to type Function.
Details:
Value=REPORT DATE
Type=Type

This is what I did ( I added "REPORT DATE") here:

Pivot = Table.Pivot(Type, List.Distinct(Type[TEST]), "TEST", "RESULT","REPORT DATE")

Is what I did wrong?

Thank you.
 
Upvote 0
ab ovo:

I hope your source data is an Excel Table
so ....
select any cell in your source table,
then
ribbon - Data tab - From Table

it will open new window (Power Query Editor)
set proper data type for each column then
select TEST column and from the ribbon - Transform - Pivot Column
choose : RESULT
Advanced options: don't aggregate - ok
then Close&Load to..
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top