Skip to main content
Question

Can i create a reverse pivot table?


craftydutchie
Forum|alt.badge.img

Hi there! I wonder if it's possible to create a list from a matrix, very much like how I'd do this in excel (see this tutorial). I currently have field names as below (there are actually 20+ years) and what i need is a separate record for each year. Is there a way of doing it? I'd do it in excel but it's not working for me for some reason - plus I'd rather incorporate it into my work flow.

I tried the attribute exploder but this became a bit of a mess.

 

Thanks!

Renee

DistrictIndustry (short)20172018201920202021Herefordshire, County ofAgriculture etc171.2176.9182.2187.9192.3

6 replies

danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Evangelist
  • January 5, 2020

Hello @reneewsp

There is a interesting link recorded video about pivot :

https://knowledge.safe.com/articles/31701/transpose-a-table-using-fme.html

 

Thanks,

Danilo

 


craftydutchie
Forum|alt.badge.img
danilo_fme wrote:

Hello @reneewsp

There is a interesting link recorded video about pivot :

https://knowledge.safe.com/articles/31701/transpose-a-table-using-fme.html

 

Thanks,

Danilo

 

Thanks @danilo_fme - yes I did read this but I couldn't work out how to "reverse" pivot - i.e. create a table with the fields District, Industry and year. I'll have another go.


takashi
Influencer
  • January 5, 2020

The screenshot below illustrates a possible way I can think of. Assuming that the years are consecutive integers. The regular expression ^\d{4}$ matches a string consisting of just four numbers.

 

0684Q00000ArKMqQAN.png

Result:

0684Q00000ArKOkQAN.png

 

Alternatively this Python script works as well.

# PythonCaller Script Example
class ReversePivoter(object):
    def __init__(self):
        pass
        
    def input(self, feature):
        beg, end = 20172021
        years, values = [], []
        for y in range(beg, end + 1):
            year = str(y)
            years.append(year)
            values.append(feature.getAttribute(year))
            feature.removeAttribute(year)
            
        for year, value in zip(years, values):
            newFeature = feature.clone()
            newFeature.setAttribute('Year', year)
            if value != None:
                newFeature.setAttribute('Value', value)
            self.pyoutput(newFeature)
        
    def close(self):
        pass

0684Q00000ArKOpQAN.png

 

Hope this helps.


bwn
Evangelist
Forum|alt.badge.img+26
  • Evangelist
  • January 6, 2020
takashi wrote:

The screenshot below illustrates a possible way I can think of. Assuming that the years are consecutive integers. The regular expression ^\d{4}$ matches a string consisting of just four numbers.

 

0684Q00000ArKMqQAN.png

Result:

0684Q00000ArKOkQAN.png

 

Alternatively this Python script works as well.

# PythonCaller Script Example
class ReversePivoter(object):
    def __init__(self):
        pass
        
    def input(self, feature):
        beg, end = 20172021
        years, values = [], []
        for y in range(beg, end + 1):
            year = str(y)
            years.append(year)
            values.append(feature.getAttribute(year))
            feature.removeAttribute(year)
            
        for year, value in zip(years, values):
            newFeature = feature.clone()
            newFeature.setAttribute('Year', year)
            if value != None:
                newFeature.setAttribute('Value', value)
            self.pyoutput(newFeature)
        
    def close(self):
        pass

0684Q00000ArKOpQAN.png

 

Hope this helps.

Yes, ListExpressionPopulator or ListPopulator are the transformers I would use too as the dedicated FME Transformers for "Reversed" pivots.


ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • January 6, 2020

It's also possible to use an attributeexploder

 


craftydutchie
Forum|alt.badge.img
ebygomm wrote:

It's also possible to use an attributeexploder

 

Thanks @ebygomm - that works beautifully simply, and thanks to @takashi 's answer i know what the ^\\d{4}$ means too. Brilliant, thanks all!


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings