Excel Help, Again

Bunt4aTriple

Member (member)
Silver Supporter
SoSH Member
Jul 15, 2005
4,386
North Yarmouth, ME
Not sure if I should repurpose an old thread or start a new one.

Let me preface this by saying that I'm a dummy, but I would like to take 4, binary columns and turn that into a single column that could have multiple values, delimited by a comma.

So this:
District1 District2 District3 District4
1 0 0 0
0 0 1 0
1 1 1 1


Would become:
District
1
3
1,2,3,4


I will dip my toe into stackoverflow, but quickly get overwhelmed. The delimited entries are where I'm getting lost.
 

Bergs

funky and cold
SoSH Member
Jul 22, 2005
21,699
Not sure if I should repurpose an old thread or start a new one.

Let me preface this by saying that I'm a dummy, but I would like to take 4, binary columns and turn that into a single column that could have multiple values, delimited by a comma.

So this:
District1 District2 District3 District4
1 0 0 0
0 0 1 0
1 1 1 1


Would become:
District
1
3
1,2,3,4


I will dip my toe into stackoverflow, but quickly get overwhelmed. The delimited entries are where I'm getting lost.
DM me your email and I'll send it over.
 

GreenMonster49

Well-Known Member
Silver Supporter
SoSH Member
Jul 18, 2005
649
Are you running Excel 365, or some other version? (Excel 365 has some text functions that make adding the commas fairly easy.)
 

luckiestman

Son of the Harpy
SoSH Member
Jul 15, 2005
32,789
I might be missing something. Why can’t they just save it as a csv file
 

GreenMonster49

Well-Known Member
Silver Supporter
SoSH Member
Jul 18, 2005
649
If you can use Excel 365, I would have two columns. E2 (which you can copy down to E3 and E4 in your example) would have the formula
=IF(A2,1,"")&IF(B2,2,"")&IF(C2,3,"")&IF(D2,4,"")

F2 would have
=TEXTJOIN(",",,MID(E2,SEQUENCE(LEN(E2)),1))
 

InstaFace

The Ultimate One
SoSH Member
Sep 27, 2016
22,151
Pittsburgh, PA
first thought: do an if() where if it's 1/True, you append the column number to an array, and then you join the array on a comma delimiter. Because otherwise, you get commas where they don't belong.

second thought: if there's a reasonable number of columns (like just 4, here), you just concatenate a string with a bunch of if() statements, like if(c3=1, ",3", "") [note that the string being concatenated is comma-number], and then remove any leading or trailing commas as the case may be.
 

tonyandpals

Well-Known Member
Lifetime Member
SoSH Member
Mar 18, 2004
7,861
Burlington
Not sure if I should repurpose an old thread or start a new one.

Let me preface this by saying that I'm a dummy, but I would like to take 4, binary columns and turn that into a single column that could have multiple values, delimited by a comma.

So this:
District1 District2 District3 District4
1 0 0 0
0 0 1 0
1 1 1 1


Would become:
District
1
3
1,2,3,4


I will dip my toe into stackoverflow, but quickly get overwhelmed. The delimited entries are where I'm getting lost.

You're kind of describing two different things. Wanting a comma delimited file, but then your expected output really isn't that (as you're dropping the 0s.

So is the expected output really as you say, or is it:

District1,District2,District3,District4
1,0,0,0
0,0,1,0
1,0,0,0
1,1,1,1

I guess a good question is, how is your expected output to be used?
 

Bergs

funky and cold
SoSH Member
Jul 22, 2005
21,699
If you can use Excel 365, I would have two columns. E2 (which you can copy down to E3 and E4 in your example) would have the formula
=IF(A2,1,"")&IF(B2,2,"")&IF(C2,3,"")&IF(D2,4,"")

F2 would have
=TEXTJOIN(",",,MID(E2,SEQUENCE(LEN(E2)),1))
Ooo. I didn't know about textjoin. THAT'S HOT.

Edit: I also didn't know about sequence. I enjoyed your solution very much!
 
Last edited:

Bergs

funky and cold
SoSH Member
Jul 22, 2005
21,699
You're kind of describing two different things. Wanting a comma delimited file, but then your expected output really isn't that (as you're dropping the 0s.

So is the expected output really as you say, or is it:

District1,District2,District3,District4
1,0,0,0
0,0,1,0
1,0,0,0
1,1,1,1

I guess a good question is, how is your expected output to be used?
Note to self: do not let @tonyandpals assemble furniture that needs directions.
 

GreenMonster49

Well-Known Member
Silver Supporter
SoSH Member
Jul 18, 2005
649
Ooo. I didn't know about textjoin. THAT'S HOT.

Edit: I also didn't know about sequence. I enjoyed your solution very much!
There's actually a simpler version. E2 could have:
=TEXTJOIN(",",TRUE,IF(A2:D2=1,COLUMN(A2:D2),""))

The problem with this is that it expects the columns that represent 1-4 to be A-D.
But if you had row 1 be 1, 2, 3, and 4 in A1 through D1, with Row 2 being the District Names, and the data starting in Row 3, then E3 (the formula for the first row) could be:
=TEXTJOIN(",",TRUE,IF(A3:D3=1,$A$1:$D$1,""))
and this will work even if you add columns to the left of the data.
 

Bergs

funky and cold
SoSH Member
Jul 22, 2005
21,699
There's actually a simpler version. E2 could have:
=TEXTJOIN(",",TRUE,IF(A2:D2=1,COLUMN(A2:D2),""))

The problem with this is that it expects the columns that represent 1-4 to be A-D.
But if you had row 1 be 1, 2, 3, and 4 in A1 through D1, with Row 2 being the District Names, and the data starting in Row 3, then E3 (the formula for the first row) could be:
=TEXTJOIN(",",TRUE,IF(A3:D3=1,$A$1:$D$1,""))
and this will work even if you add columns to the left of the data.
Stop laughing at me.
 

GreenMonster49

Well-Known Member
Silver Supporter
SoSH Member
Jul 18, 2005
649
Quoted for truth...
There's actually a simpler version. E2 could have:
=TEXTJOIN(",",TRUE,IF(A2: D2=1,COLUMN(A2: D2),""))

The problem with this is that it expects the columns that represent 1-4 to be A-D.
But if you had row 1 be 1, 2, 3, and 4 in A1 through D1, with Row 2 being the District Names, and the data starting in Row 3, then E3 (the formula for the first row) could be:
=TEXTJOIN(",",TRUE,IF(A3: D3=1,$A$1:$D$1,""))
and this will work even if you add columns to the left of the data.
 

Bunt4aTriple

Member (member)
Silver Supporter
SoSH Member
Jul 15, 2005
4,386
North Yarmouth, ME
You're kind of describing two different things. Wanting a comma delimited file, but then your expected output really isn't that (as you're dropping the 0s.

So is the expected output really as you say, or is it:

District1,District2,District3,District4
1,0,0,0
0,0,1,0
1,0,0,0
1,1,1,1

I guess a good question is, how is your expected output to be used?
Yeah. My use of delimited is throwing things off. The target column would be a string with commas. But to be fair, I did state that I was a dummy in the OP so give me a break, please!
 

GreenMonster49

Well-Known Member
Silver Supporter
SoSH Member
Jul 18, 2005
649
Okay, this is near perfect for what I need. One last ask: I do have a row where all four districts are zeros. This leaves column E blank. In that scenario, can I conditionally make column F blank as well?
If you're using my first solution (with the SEQUENCE), what you can do is to have the F2 formula be
=IFERROR(TEXTJOIN(",",,MID(E2,SEQUENCE(LEN(E2)),1)),"")

The IFERROR outputs the empty string when the TEXTJOIN part generates an error.
 

Bunt4aTriple

Member (member)
Silver Supporter
SoSH Member
Jul 15, 2005
4,386
North Yarmouth, ME
If you're using my first solution (with the SEQUENCE), what you can do is to have the F2 formula be
=IFERROR(TEXTJOIN(",",,MID(E2,SEQUENCE(LEN(E2)),1)),"")

The IFERROR outputs the empty string when the TEXTJOIN part generates an error.
Perfect! Thanks for making me look wicked smart.
 

BroodsSexton

Member
SoSH Member
Feb 4, 2006
12,647
guam
Happy Father's Day.

I would like to take an export of transactions from a bank account, and sum up the monthly debits and monthly credits, going back two years. I have date (call it column A), debit (column b), and credit (column C) columns. Any thoughts on how to do this without breaking my head?
 

gammoseditor

also had a stroke
SoSH Member
Jul 17, 2005
4,230
Somerville, MA
Happy Father's Day.

I would like to take an export of transactions from a bank account, and sum up the monthly debits and monthly credits, going back two years. I have date (call it column A), debit (column b), and credit (column C) columns. Any thoughts on how to do this without breaking my head?
A pivot table is great for this. Under insert select pivot table. It should default to select your range and create in a new sheet. Drag the date selection to the row labels box. Drag both the debits and credits to the sum values. The date parameters may default to years. But you can change it to months. I think you just right click and select months for that but not 100% sure.
 

luckiestman

Son of the Harpy
SoSH Member
Jul 15, 2005
32,789
It’s pivot tables, just to have better instructions, here is the bot walk thru


You can use a pivot table in Excel to easily sum up the monthly debits and credits. Here's how you can do it:
1. Select the data range including the headers (columns A, B and C).
2. Click on the `Insert` tab and then click on `Pivot Table`.
3. In the `Create PivotTable` dialog box, make sure the `Select a table or range` option is selected and that the `Table/Range` field shows the correct data range.
4. Choose where you want the pivot table report to be placed and click `OK`.
5. In the PivotTable Fields pane, drag the `Date` field to the `Rows` area, and the `Debit` and `Credit` fields to the `Values` area.
6. Right-click on any date in the pivot table and select `Group`. In the `Group By` dialog box, select `Months` and `Years`, then click `OK`.

This will group your data by month and year, and show you the sum of debits and credits for each month. You can then filter the data to only show the last two years.
 

BroodsSexton

Member
SoSH Member
Feb 4, 2006
12,647
guam
A pivot table is great for this. Under insert select pivot table. It should default to select your range and create in a new sheet. Drag the date selection to the row labels box. Drag both the debits and credits to the sum values. The date parameters may default to years. But you can change it to months. I think you just right click and select months for that but not 100% sure.
dammit you're speaking a language I almost recognize.
 

BroodsSexton

Member
SoSH Member
Feb 4, 2006
12,647
guam
It’s pivot tables, just to have better instructions, here is the bot walk thru


You can use a pivot table in Excel to easily sum up the monthly debits and credits. Here's how you can do it:
1. Select the data range including the headers (columns A, B and C).
2. Click on the `Insert` tab and then click on `Pivot Table`.
3. In the `Create PivotTable` dialog box, make sure the `Select a table or range` option is selected and that the `Table/Range` field shows the correct data range.
4. Choose where you want the pivot table report to be placed and click `OK`.
5. In the PivotTable Fields pane, drag the `Date` field to the `Rows` area, and the `Debit` and `Credit` fields to the `Values` area.
6. Right-click on any date in the pivot table and select `Group`. In the `Group By` dialog box, select `Months` and `Years`, then click `OK`.

This will group your data by month and year, and show you the sum of debits and credits for each month. You can then filter the data to only show the last two years.
OK, this is very close. But it's just counting the number of transactions in each month. It's not giving me the cash flow, which is what I need.


66126

OK--I got it. Change the field value from "count" to "sum" and then add another column that gives me the monthly cash flow by adding the two up. Nice. SoSH at its best... Thank you.
 
Last edited:

Rick Burlesons Yam Bag

Internet Cowboy, Turbo Accelerator, tOSU Denier
Lifetime Member
SoSH Member
If you can use Excel 365, I would have two columns. E2 (which you can copy down to E3 and E4 in your example) would have the formula
=IF(A2,1,"")&IF(B2,2,"")&IF(C2,3,"")&IF(D2,4,"")

F2 would have
=TEXTJOIN(",",,MID(E2,SEQUENCE(LEN(E2)),1))
How is TEXTJOIN different from CONCATENATE? It seems like the syntax is slightly different but the function and outcome are the same.
 

Bergs

funky and cold
SoSH Member
Jul 22, 2005
21,699
How is TEXTJOIN different from CONCATENATE? It seems like the syntax is slightly different but the function and outcome are the same.
In Excel, both the TEXTJOIN and CONCATENATE functions are used to combine text from multiple cells into a single cell. However, there are some differences between them:

  1. Functionality: CONCATENATE is a basic function that concatenates (joins) multiple text strings together. It takes individual text strings or cell references as arguments and combines them into a single string. It doesn't provide any additional options for delimiters or ignoring empty cells.
    TEXTJOIN, on the other hand, is a more versatile function introduced in Excel 2016 (and later versions) that offers additional capabilities. It allows you to join text strings using a specified delimiter and can also ignore empty cells during the concatenation process.
  2. Delimiters: CONCATENATE doesn't have built-in support for delimiters. If you want to include a delimiter between the concatenated text strings, you need to manually add it as an argument within quotation marks.
    TEXTJOIN, however, allows you to specify a delimiter that is automatically inserted between the joined text strings. This eliminates the need for manual insertion and provides more control over the output.
  3. Ignoring empty cells: CONCATENATE doesn't have a built-in option to skip or ignore empty cells during concatenation. If any of the referenced cells are empty, CONCATENATE includes an empty string in the final result.
    TEXTJOIN includes an optional argument that allows you to ignore empty cells. By specifying the "ignore_empty" argument as TRUE, TEXTJOIN excludes empty cells from the concatenation process. This can be useful when dealing with datasets that may contain empty cells.
Overall, TEXTJOIN is a more powerful function for joining text in Excel, offering delimiter support and the ability to ignore empty cells. It provides greater flexibility and convenience compared to the CONCATENATE function, particularly in scenarios where you need more control over the concatenation process.


Edit: ChatGPT is fun.
 

Rick Burlesons Yam Bag

Internet Cowboy, Turbo Accelerator, tOSU Denier
Lifetime Member
SoSH Member
In Excel, both the TEXTJOIN and CONCATENATE functions are used to combine text from multiple cells into a single cell. However, there are some differences between them:

  1. Functionality: CONCATENATE is a basic function that concatenates (joins) multiple text strings together. It takes individual text strings or cell references as arguments and combines them into a single string. It doesn't provide any additional options for delimiters or ignoring empty cells.
    TEXTJOIN, on the other hand, is a more versatile function introduced in Excel 2016 (and later versions) that offers additional capabilities. It allows you to join text strings using a specified delimiter and can also ignore empty cells during the concatenation process.
  2. Delimiters: CONCATENATE doesn't have built-in support for delimiters. If you want to include a delimiter between the concatenated text strings, you need to manually add it as an argument within quotation marks.
    TEXTJOIN, however, allows you to specify a delimiter that is automatically inserted between the joined text strings. This eliminates the need for manual insertion and provides more control over the output.
  3. Ignoring empty cells: CONCATENATE doesn't have a built-in option to skip or ignore empty cells during concatenation. If any of the referenced cells are empty, CONCATENATE includes an empty string in the final result.
    TEXTJOIN includes an optional argument that allows you to ignore empty cells. By specifying the "ignore_empty" argument as TRUE, TEXTJOIN excludes empty cells from the concatenation process. This can be useful when dealing with datasets that may contain empty cells.
Overall, TEXTJOIN is a more powerful function for joining text in Excel, offering delimiter support and the ability to ignore empty cells. It provides greater flexibility and convenience compared to the CONCATENATE function, particularly in scenarios where you need more control over the concatenation process.


Edit: ChatGPT is fun.
Thanks for this. The Empty cell option doesn't come up in 99% of my use cases, but this would be useful there.