Skip to main content
Question

Can i create a reverse pivot table?

  • January 5, 2020
  • 6 replies
  • 69 views

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
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

6 replies

danilo_fme
Celebrity
Forum|alt.badge.img+51
  • Celebrity
  • 2077 replies
  • 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
  • Author
  • 8 replies
  • 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

 

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
Celebrity
  • 7843 replies
  • 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 = 2017, 2021
        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
  • 562 replies
  • January 6, 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 = 2017, 2021
        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+44
  • Influencer
  • 3427 replies
  • January 6, 2020

It's also possible to use an attributeexploder

 


craftydutchie
Forum|alt.badge.img
  • Author
  • 8 replies
  • January 6, 2020

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!