Table sorted by match and frequency

ilegall

New Member
Joined
Jan 5, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

would like to ask for help on this. I need to sort the table (new column with both new and theoretically repetitive tags added weekly) by match and frequency. Any idea how to do that?

Example:
first line - tag in row matches, most frequent amongst all columns
second line - tag in row matches, 2nd most frequent...

More advanced would then be showing also the frequency count of each tag row.

Note that the list provided weekly is randomly sorted and the function to sort manually every week needs to be kept.

Thank you very much for the help!

-ilegall


1672935053393.png
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Welcome to the MrExcel forum!

We need a bit more information. Is that an actual Excel table, or is it just data formatted to look like a table? Are there any other columns in it that don't contain "tags"? (I assume a tag is any of the values in the table, like 2HS-141, which occurs in 3 columns, or 11-LSH 9005 which occurs in 2).

It sounds like you want to create a list of unique tags, sorted by frequency. So 2HS-141 would be ahead of 11-LSH 9005. Do you want this list as a column in the table, or somewhere else? Do you want each individual column sorted? By the list to-date, or the complete list?

Are you looking for something that can be done with formulas, or with VBA (the Excel macro language)?

What version of Excel do you have? It would be helpful if you could update you profile to reflect that. Different versions have different options.

Also, since you're new, I'd like to suggest you look into the XL2BB tool (see a link in my signature or the reply box). This allows you to display your sample data in such a way that the helpers here can copy and paste your data. Displaying a picture like you did requires us to retype everything, and some people won't take the time to do that.


Here's one possibility if you have Excel 365:

Book1
ABCDEFGH
110/20/202212/22/202212/29/20221/5/2023TagFrequency
211-LSH 900511-LSH 90059-PI-7123C3PIC-68302HS-1414
311-UA-91013UA-83022HS-1414TI-2-393GO-2007A4
42HS-141UA3120-23GO-2007AUA3120-23PIC-68304
53GO-2007A4UA-35266IL501-13SI-3535B3TI-89394
63HS-6043A4LSL-80213PIC-68309-PI-7123C4FSAH91254
73JI-6197B3PIC-6830UA3120-23LI-7345UA3120-24
83PIC-68303TI-8939F20153TI-89396FR10124
93QAH-9112DEV4UA-101K101 HP PIANO3GO-2007A3UA-83023
103QI-1850-29-PI-7123C3TI-89394UA-35269-PI-7123C3
113QI-2140F20154FSAH912511-TIC 966111-LSH 90052
123QZAH-21403GO-2007A6FR101209TAH-39144TI-2-392
133TI-89396FR10123UA-83024FSAH9125ZL37112
144FSAH91252HS-14106HS-31303UA-83024UA-35262
154PDI-34613SI-3535B11-TIC 966106HS-3130F20152
164TI-2-394UA-9920ZL37112HS-1413SI-3535B2
17AI7602B3FY-6903B02QI-25106HS-31302
18HS-6075A4FSAH91254PDI-352511-TIC 96612
19PDI-6833A6FR101211-UA-91011
20UA3120-29-HS-7120A-23HS-6043A1
21ZL37116FR10123JI-6197B1
223QAH-9112DEV1
233QI-1850-21
243QI-21401
253QZAH-21401
264PDI-34611
27AI7602B1
28HS-6075A1
29PDI-6833A1
304LSL-80211
314UA-1011
324UA-99201
333FY-6903B1
346IL501-11
35K101 HP PIANO1
363LI-73451
3709TAH-39141
3802QI-2511
394PDI-35251
409-HS-7120A-21
Sheet6
Cell Formulas
RangeFormula
G2:H40G2=LET(t,Table1[#Data],r,ROWS(t),c,COLUMNS(t),s,SEQUENCE(r*c,,0),a,INDEX(t,MOD(s,r)+1,INT(s/r)+1),b,UNIQUE(FILTER(a,a<>"")),ci,COUNTIF(t,b),CHOOSE({1,2},SORTBY(b,ci,-1),SORT(ci,,-1)))
Dynamic array formulas.


I believe that formula can be shortened to

Excel Formula:
=LET(t,Table1[#Data],a,UNIQUE(TOCOL(t,1)),ci,COUNTIF(t,a),CHOOSE({1,2},SORTBY(a,ci,-1),SORT(ci,,-1)))


if you have the TOCOL function.
 
Upvote 0
Hi Eric, thanks for the quick reply and warm welcome. :-)

Data are currently formatted as table, but this can be changed back to just set of data as requested for the actual data formatting formula/macro. So either script or formula solution works with me.

That is correct, you understand the problem very clearly, I want to create to a list of unique tags (remove duplicates if more than one occurrence in 1 column), sorted by frequency, BUT it is vital to include and track week of occurence (leave blank if missing that week) and make every column sorted to match this criteria as you suggested, so the table/set of data would look something like attached below via suggested xl2bb tool. Would that be managable?

Hope I explained myself clearly enough thanks a lot for the help!

-ilegall

Tag data tracking idea.xlsx
BCDEF
24Frequency20/10/202222/12/202229/12/20225/1/2023
2542HS-1412HS-1412HS-1412HS-141
2643GO-2007A3GO-2007A3GO-2007A3GO-2007A
2743PIC-68303PIC-68303PIC-68303PIC-6830
2843TI-89393TI-89393TI-89393TI-8939
2944FSAH91254FSAH91254FSAH91254FSAH9125
304UA3120-2UA3120-2UA3120-2UA3120-2
3136FR10126FR10126FR1012
3233UA-83023UA-83023UA-8302
3339-PI-7123C9-PI-7123C9-PI-7123C
34211-LSH 900511-LSH 9005
3524TI-2-394TI-2-39
362ZL3711ZL3711
Active Alarms
 
Upvote 0
I'm sure this could be cleaner, but it seems to do the job. Open a copy of your workbook. Press Alt-F11 to open the VBA editor. Press Alt-IM to Insert a Module. Paste the following code into the window that opens:

VBA Code:
Sub SortTags()
Dim MyTags As Range, Results As Range, dict As Object, mr As Long, mc As Long
Dim c As Long, r As Long, i As Long, ColData() As Variant, MyData As Variant
Dim keyz As Variant, itemz As Variant, Table1() As Variant, Table2() As Variant

    Set MyTags = Sheets("Sheet1").Range("A1")
    Set Results = Sheets("Sheet2").Range("A1")
    
    Set dict = CreateObject("Scripting.Dictionary")
    mr = 0
    mc = 0
    While MyTags.Offset(0, mc) <> ""
        mr = WorksheetFunction.Max(mr, MyTags.Offset(Rows.Count - MyTags.Row, mc).End(xlUp).Row)
        mc = mc + 1
    Wend

    MyData = MyTags.Resize(mr - MyTags.Row + 1, mc).Value
    ReDim ColData(1 To mc)
    
    For c = 1 To UBound(MyData, 2)
        Set ColData(c) = CreateObject("Scripting.Dictionary")
        For r = 2 To UBound(MyData)
            If MyData(r, c) <> "" Then
                dict(MyData(r, c)) = dict(MyData(r, c)) + 1
                ColData(c)(MyData(r, c)) = ColData(c)(MyData(r, c)) + 1
            End If
        Next r
    Next c
    
    keyz = dict.keys
    itemz = dict.items
    ReDim Table1(1 To dict.Count, 1 To 2)
    For i = 1 To dict.Count
        Table1(i, 1) = keyz(i - 1)
        Table1(i, 2) = itemz(i - 1)
    Next i
    
    Table1 = WorksheetFunction.Sort(Table1, Array(2, 1), Array(-1, 1))
    
    ReDim Table2(0 To dict.Count, 0 To mc)
    Table2(0, 0) = "Frequency"
    For r = 1 To dict.Count
        Table2(r, 0) = Table1(r, 2)
    Next r
    For c = 1 To mc
        Table2(0, c) = MyData(1, c)
        For r = 1 To UBound(Table2)
            If ColData(c).exists(Table1(r, 1)) Then Table2(r, c) = Table1(r, 1)
        Next r
    Next c
    
    Results.Resize(UBound(Table2) + 1, UBound(Table2, 2) + 1) = Table2
        
    
End Sub

On the first 2 executable lines ("Set MyTags = " and "Set Results = "), change them to be the upper left corner of the input and output areas. On the input it searches as far right and down until it finds an empty cell. You can write to the same place if you want, but you'll have to allow for the inserted column of frequencies.

Once that's done, go back to Excel. Press Alt-F8 to open the macro selector, select SortTags and click Run. Given the input from post #2, this is what the macro created:

Book1
ABCDE
1Frequency10/20/202212/22/202212/29/20221/5/2023
242HS-1412HS-1412HS-1412HS-141
343GO-2007A3GO-2007A3GO-2007A3GO-2007A
443PIC-68303PIC-68303PIC-68303PIC-6830
543TI-89393TI-89393TI-89393TI-8939
644FSAH91254FSAH91254FSAH91254FSAH9125
746FR10126FR10126FR1012
84UA3120-2UA3120-2UA3120-2UA3120-2
933UA-83023UA-83023UA-8302
1039-PI-7123C9-PI-7123C9-PI-7123C
11206HS-313006HS-3130
12211-LSH 900511-LSH 9005
13211-TIC 966111-TIC 9661
1423SI-3535B3SI-3535B
1524TI-2-394TI-2-39
1624UA-35264UA-3526
172F2015F2015
182ZL3711ZL3711
19102QI-251
20109TAH-3914
21111-UA-9101
2213FY-6903B
2313HS-6043A
2413JI-6197B
2513LI-7345
2613QAH-9112DEV
2713QI-1850-2
2813QI-2140
2913QZAH-2140
3014LSL-8021
3114PDI-3461
3214PDI-3525
3314UA-101
3414UA-9920
3516IL501-1
3619-HS-7120A-2
371AI7602B
381HS-6075A
391K101 HP PIANO
401PDI-6833A
Sheet2


Let me know what you think.
 
Upvote 0
As a formula approach, would this be headed in the right direction?

ilegall.xlsm
ABCDEFGHIJK
110/20/202212/22/202212/29/20221/05/2023
211-LSH 900511-LSH 90059-PI-7123C3PIC-68302HS-14142HS-1412HS-1412HS-1412HS-141
311-UA-91013UA-83022HS-1414TI-2-393GO-2007A43GO-2007A3GO-2007A3GO-2007A3GO-2007A
42HS-141UA3120-23GO-2007AUA3120-23PIC-683043PIC-68303PIC-68303PIC-68303PIC-6830
53GO-2007A4UA-35266IL501-13SI-3535B3TI-893943TI-89393TI-89393TI-89393TI-8939
63HS-6043A4LSL-80213PIC-68309-PI-7123C4FSAH912544FSAH91254FSAH91254FSAH91254FSAH9125
73JI-6197B3PIC-6830UA3120-23LI-73456FR101246FR10126FR10126FR1012
83PIC-68303TI-8939F20153TI-8939UA3120-24UA3120-2UA3120-2UA3120-2UA3120-2
93QAH-9112DEV4UA-101K101 HP PIANO3GO-2007A3UA-830233UA-83023UA-83023UA-8302
103QI-1850-29-PI-7123C3TI-89394UA-35269-PI-7123C39-PI-7123C9-PI-7123C9-PI-7123C
113QI-2140F20154FSAH912511-TIC 966106HS-3130206HS-313006HS-3130
123QZAH-21403GO-2007A6FR101209TAH-391411-LSH 9005211-LSH 900511-LSH 9005
133TI-89396FR10123UA-83024FSAH912511-TIC 9661211-TIC 966111-TIC 9661
144FSAH91252HS-14106HS-31303UA-83023SI-3535B23SI-3535B3SI-3535B
154PDI-34613SI-3535B11-TIC 966106HS-31304TI-2-3924TI-2-394TI-2-39
164TI-2-394UA-9920ZL37112HS-1414UA-352624UA-35264UA-3526
17AI7602B3FY-6903B02QI-251F20152F2015F2015
18HS-6075A4FSAH91254PDI-3525ZL37112ZL3711ZL3711
19PDI-6833A6FR101202QI-251102QI-251
20UA3120-29-HS-7120A-209TAH-3914109TAH-3914
21ZL37116FR101211-UA-9101111-UA-9101
223FY-6903B13FY-6903B
233HS-6043A13HS-6043A
243JI-6197B13JI-6197B
253LI-734513LI-7345
263QAH-9112DEV13QAH-9112DEV
273QI-1850-213QI-1850-2
283QI-214013QI-2140
293QZAH-214013QZAH-2140
304LSL-802114LSL-8021
314PDI-346114PDI-3461
324PDI-352514PDI-3525
334UA-10114UA-101
344UA-992014UA-9920
356IL501-116IL501-1
369-HS-7120A-219-HS-7120A-2
37AI7602B1AI7602B
38HS-6075A1HS-6075A
39K101 HP PIANO1K101 HP PIANO
40PDI-6833A1PDI-6833A
Sheet1
Cell Formulas
RangeFormula
F2:G40F2=LET(t,UNIQUE(TOCOL(Table1,1)),SORT(SORT(CHOOSE({1,2},t,COUNTIF(Table1,t)),1),2,-1))
H2:K40H2=LET(v,INDEX($F2#,0,1),IF(ISNUMBER(MATCH(v,INDEX(Table1,0,COLUMNS($H:H)),0)),v,""))
Dynamic array formulas.
 
Upvote 0
Solution
Erik and Peter, thank you both so much for the effort! I managed to get Peter's formula approach working for this purpose without much extra work every week.

I will ask one more question though to close the automation loop here. This will require most definitely require VBA now. If I am exporting this set of data from another automatically generated sheet week by week, what would be the easiest solution to automate the import and add simplified date (DD/MM/YYYY) on top of each dataset as shown above?

Thanks!

-ilegall
 
Upvote 0
Are you talking about putting the dates into A1:D1 as in my mini sheet above or are you talking about getting the dates from A1:D1 into H1:K1?
 
Upvote 0
A1:D1 and so forth per each week's import. This could then be simply copied into H1:K1 and more with a simple =cellValue formula, right?
 
Upvote 0
That still is not entirely clear to me, but does this formula in cell H1 in my layout above do what you want?

Excel Formula:
=Table1[#Headers]
 
Upvote 0
Apologies for the confusion. To clarify: I want to avoid manual copying of data (20 rows you see above) from automatically generated excel sheet to my master data sheet compiling all these automated report's data on a weekly basis. At this point I copy only the 20 rows, not the date above it, which I then add manually per report date. Any chance this import + date on top addition could be automated or semi-automated? Thanks Peter!

-ilegall
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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