Skip to main content

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

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

 


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.


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 = e], b]
        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.


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 = e], b]
        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.


It's also possible to use an attributeexploder

 


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!


Reply